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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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
 Share

×
×
  • Create New...