Sign in to follow this  
Followers 0
ConsultingJoe

Search String Within 2 Strings

7 posts in this topic

I want to be able to find the string between two other strings

$string = "hello there guys"

$start = "hello "

$end = " guys"

The result should be "there"

This is what I tried:

Func _searchtext($string, $start, $end)
    $startpos = StringInStr ( $string, $start, 0, 1 )
    If $startpos = 0 Then Return 0
    $endpos = StringInStr ( $string, $end, 0, 1 )
    If $endpos = 0 Then Return 0
    $startlen = StringLen ( $start ) 
    $endlen = StringLen ( $end )
    $string = StringLeft($string, $endpos)
    $stringlen = StringLen ( $string )
    $string = StringRight($string, $stringlen-($startpos+$startlen) )
        ConsoleWrite($string)
    ConsoleWrite(@CRLF)
    return $string
EndFunc
$string = "123456789"
$start = "12"
$end = "56"


ConsoleWrite(_searchtext( $string, $start, $end ))

It should be easy right? well aperently not for me.

Thanks guys


[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Share this post


Link to post
Share on other sites



look for stringbetween in "Autoit Wrappers"...

in my sig

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

Hi,

here are some different versions.

http://www.autoitscript.com/forum/index.ph...18372&hl=String

So long,

Mega

P.S.: I still want to see it in a beta. :)


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

StringRegExp() would be faster if you are doing large strings:

$string = 'I like pizza.  I need pizza.'

$blah = _SRE_Between($string, 'I', 'pizza', 1)
If IsArray($blah) Then
    For $i = 0 To UBound($blah) - 1
        MsgBox(0, '', $blah[$i])
    Next
Else
    MsgBox(0, '', $blah)
EndIf

Func _SRE_Between($s_String, $s_Start, $s_End, $i_ReturnArray = 0); $i_ReturnArray returns an array of all found if it = 1, otherwise default returns first found
    $a_Array = StringRegExp($s_String, '(?:' & $s_Start & ')(.*?)(?:' & $s_End & ')', 3)
    If Not @error And Not $i_ReturnArray And IsArray($a_Array) Then Return $a_Array[0]
    If IsArray($a_Array) Then Return $a_Array
EndFunc

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Another way:

;-------------------------------------------------
;==>_StringBetween
;-------------------------------------------------
Func _StringBetween($string, $begin, $end)
    Local $_begin, $_end
    $_begin = StringSplit($string, $begin, 1)
    $_end = StringSplit($_begin[2], $end, 1)
    If Not @error Then
        Return ($_end[1])
    EndIf
EndFunc  ;==>_StringBetween

Share this post


Link to post
Share on other sites

Another way:

;-------------------------------------------------
;==>_StringBetween
;-------------------------------------------------
Func _StringBetween($string, $begin, $end)
    Local $_begin, $_end
    $_begin = StringSplit($string, $begin, 1)
    $_end = StringSplit($_begin[2], $end, 1)
    If Not @error Then
        Return ($_end[1])
    EndIf
EndFunc ;==>_StringBetween
Thanks a lot guys for all your help. I would use this often. I'm just going to use this one because its so simple.

Thanks man and welcome to the forum


[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Thanks a lot guys for all your help. I would use this often. I'm just going to use this one because its so simple.

Thanks man and welcome to the forum

Thanks to you and to all people who contribute to AutoIt's growth!

BTW: another usefull function: _StringReplace.

It finds the string between two other strings and replaces it with another one. If no delimiters were found, the original string is returned.

;-------------------------------------------------
;==>_StringReplace
;-------------------------------------------------
; flag = 0 --> not case sensitive
; flag = 1 --> case sensitive
Func _StringReplace($string, $begin, $end, $replace, $flag)
    Local $_begin, $_end
    If StringInStr($string, $begin, $flag) <> 0 And StringInStr($string, $end, $flag) <> 0 Then
        $_begin = StringSplit($string, $begin, 1)
        $_end = StringSplit($string, $end, 1)
        $newstring = $_begin[1] & $begin & $replace & $end & $_end[2]
        Return ($newstring)
    Else
        Return ($string)
    EndIf
EndFunc;==>_StringReplace
Edited by tittoproject

Share this post


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
Sign in to follow this  
Followers 0