Jump to content

how to extract a special string from a long string


Recommended Posts

Hello Members of this best Forum
i have a question please
for example if i have a long string
and i want to extract a text between two tag
what i can do to make that?
note :
i know that there is a

StringRegExp

function
it's do that work
but it result is be as an array
i want the result to be a string
is there any function on autoit can do that?
Thanks in advance.

Link to post
Share on other sites
  • Developers
51 minutes ago, nacerbaaziz said:

it's do that work
but it result is be as an array
i want the result to be a string

What is the issue of taking the value from the array and putting that into a string?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites

I personally use in almost all my projects this simple wrapper for _StringBetween() to get function which returns string and not array:

Func StringBetween($sString, $sStart = '', $sEnd = '')
    Local $sReturn
    $sReturn = _StringBetween($sString, $sStart, $sEnd)
    If @error Then Return ''
    Return $sReturn[0]
EndFunc

 

EDIT: In fact I use this more complex modification:

Func StringBetween($sString, $sStart = '', $sEnd = '')
    Local $sReturn, $i
    If $sStart == '' And $sEnd == '' Then Return ''

    If $sStart == '' Then
        $sReturn = StringLeft($sString, StringInStr($sString, $sEnd)-1)
    ElseIf $sEnd == '' Then
        $i = StringInStr($sString, $sStart)
        If $i > 0 Then
            $sReturn = StringMid($sString, $i + StringLen($sStart))
        Else
            $sReturn = ''
        EndIf
    Else
        $sReturn = _StringBetween($sString, $sStart, $sEnd) ;, -1, 1)
        If @error Then Return ''
        $sReturn = $sReturn[0]
    EndIf
    Return $sReturn
EndFunc

 

Edited by Zedna
Link to post
Share on other sites

hi again
I succeeded with the following code

#include <Array.au3>
#include <String.au3>
$text = "<easyAudioPlayer>easy Audio Player what's new?</easyAudioPlayer><easyAudioPlayerUpdate>Update version 3.0.0</EasyAudioPlayerUpdate>"
msgBox(0, "", _arrayToString(_StringBetween($text, "<easyAudioPlayer>", "</easyAudioPlayer>")))


dear #Earthshine
Thanks for letting me know about this function
I did not know it before
i mean the _StringBetween function

dear #Jos
I did not know the _arrayToString function
Thanks for alerting me
dear #Zedna
your function is also work with me. thank you very much
(
I have another request from you please
when i post a autoit code i using the (code=autoit) and the (/code) between the ([]) tags
but it not working with me. where is the mistake here please?
because i am blind and the code field in the autoit forum isn't accessible with the screen reader programs
)
thank you to all of you

Link to post
Share on other sites
#include <String.au3>

$text = "<easyAudioPlayer>easy Audio Player what's new?</easyAudioPlayer><easyAudioPlayerUpdate>Update version 3.0.0</EasyAudioPlayerUpdate>"

msgBox(0, "", StringBetween($text, "<easyAudioPlayer>", "</easyAudioPlayer>"))

Func StringBetween($sString, $sStart, $sEnd)
    Local $sReturn
    $sReturn = _StringBetween($sString, $sStart, $sEnd)
    If @error Then Return ''
    Return $sReturn[0]
EndFunc

 

Edited by Zedna
Link to post
Share on other sites

in the case provided where the match exists, and the first match is the desired match, you have two options without UDF.  The commented option is regex since you only ever need element 0, you could always end with a [0] and use the stored string without much concern for the array.  The second example (and my favorite on the menu) is an appetizing stringop salad.

$text = "<easyAudioPlayer>easy Audio Player what's new?</easyAudioPlayer><easyAudioPlayerUpdate>Update version 3.0.0</EasyAudioPlayerUpdate>"

;~ msgbox(0, '' , StringRegExp($text, "<easyAudioPlayer>(.*?)</easyAudioPlayer>" , 3)[0])

msgbox(0, '' , stringmid($text , StringInStr($text , '<easyAudioPlayer>') + stringlen('<easyAudioPlayer>') , StringInStr($text , '</easyAudioPlayer>') - stringlen('</easyAudioPlayer>')))

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Colduction
      Hi again guys!, i had COVID-19 for twice and i couldn't check the forum since 3 or 4 months ago till now! i hope you will get better if you're fighting for beat COVID-19

      I have two question, first is about extracting all of the IP Address from an IP Ranges, for e.g: 192.168.1.1-192.255.255.255 (Start and End are variable and will be defined by the user) and for second one, i have a friend that he is Python programmer, he made a IP Parser that it can support large txt files (1TB) and it can parse all of them under 10min and it also supports low-end PCs that have 1 GB RAM!

      The list that his program parses are:
      #1765497 192.168.1.1 8082 #1765496 192.168.1.1 8084 #1965493 192.168.1.1 8089 #9565495 192.168.1.1 8086 #2565492 192.168.1.1 8081 and it converts very very fast to this:
      192.168.1.1:8082 192.168.1.1:8084 192.168.1.1:8089 192.168.1.1:8086 192.168.1.1:8081 I wonder how to do this via AutoIt, if you can help me in this way, i will be happy✌❤

      Thanks for your helps.

      FIRST_QUESTION_TEST.au3
    • By cruisepandey
      Hi, 
      I have a string like this : 
      Global $Msga = "urrent directory is /send.  (Submission of file with log number 29381077284 is confirmed)";
      I want to extract the number 29381077284  from the string. I did StringSplit to split based on "(" and then use space to reach there, But it's not a good choice. 
      Can anyone help me with regular expression to find the number from String using AutoIT. TIA
    • By DannyJ
      I have this example string:
       
      2020-06-08 09:23:33 : abcdefghifjklm How to trim just the right part? Or how to trim this text right to left?
      I want to trim this string right to left until ' : '.
      In ohter words: I need the 'abcdefghifklm' text.
      I have tried StringTrimRight, but that requires 
      The number of characters to trim. Unfortunately StringSplit trims the left part of the strings.
       
       
    • By careca
      This is my take on string triggers, triggers on specific strings.
      Able to simple text pasting,
      opening links (as long as there's a www. http:\\ or https:\\ at the beggining)
      and is able to open applications, if there is a parameter in the parameter field, it uses it.
      Shows your external, lan, and gateway ip's.
      Able to change system volume by a set percentage, reading from the inputbox the number the user sets, if 0 or empty uses system default.
      I made this because the existing string trigger applications didn't do it for me.
      I did this for me, but if someone finds it useful, all the better.
    • By Colduction
      Hi guys!, i have a problem to convert Python code to AutoIt code, in fact i had not coded with Python yet!, this code is about permutation a string's case, i will be happy with your comments :)❤;

      Python code:
       
      # Python code to print all permutations # with respect to cases # Function to generate permutations def permute(inp): n = len(inp) # Number of permutations is 2^n mx = 1 << n # Converting string to lower case inp = inp.lower() # Using all subsequences and permuting them for i in range(mx): # If j-th bit is set, we convert it to upper case combination = [k for k in inp] for j in range(n): if (((i >> j) & 1) == 1): combination[j] = inp[j].upper() temp = "" # Printing current combination for i in combination: temp += i print(temp), # Driver code permute("Hello") # This code is contributed by Sachin Bisht
      My code in AutoIt:
      ; https://www.geeksforgeeks.org/permute-string-changing-case/ _PermuteCase("ABC") Func _PermuteCase($sText) If StringRegExp($sText, "^[A-Za-z]{1,}$") Then Local $iLength = StringLen($sText) ; Get length of the text. Local $iMaxPerm = 2 ^ $iLength ; Number of permutations is 2^n Local $sLow_Text = StringLower($sText) ; Converting string to lower case Local $asChrs = StringToASCIIArray($sLow_Text) ; Split the text into array of chars. For $i = 1 To $iMaxPerm Step 1 For $j = 0 To $asChrs[0] ;................................................... Next Next Else Return SetError(-1, 0, "Error: Input is incorrect!") EndIf EndFunc ;==>_PermuteCase  
       
       
       
       
       
      ====================== SOLUTION by @TheXman ======================
       
×
×
  • Create New...