VIP

[SOLVED] _StringBetween

5 posts in this topic

#1 ·  Posted (edited)

I would like to take String Between of the text, on starting point and end point found.

#include <String.au3>
Local $begin = "(("
Local $end = "))"
Local $loop = 0
For $i = 1 To 2
    While 1
        $loop += 1
        $begin &= "("
        If $loop = $i Then ExitLoop
    WEnd
    $loop = 0
    While 1
        $loop += 1
        $end &= ")"
        If $loop = $i Then ExitLoop
    WEnd
    Local $keyS = "#", $keyE = "@"
    Local $0 = "A))" & $begin & $keyS & "(" & $end & $begin & $begin & "C" & $end & $begin & $end & ")" & $keyE & $end & "(((B"
    Local $A = _StringBetween($0, $keyS, $keyE)
    Local $C = $keyS & $A[0] & $keyE
    Local $B = _StringBetween($0, $begin, $end)
    Local $D = $B[0]
    ConsoleWrite("!-IN: " & $0 & @CRLF)
    ConsoleWrite("String Between: " & $begin & "  <with>  " & $end & @CRLF)
    If $C = $D Then
        ConsoleWrite("+OUT OK: " & $D & @CRLF & @CRLF)
    Else
        ConsoleWrite("- True result: " & $C & @CRLF)
        ConsoleWrite("!     But OUT: " & $D & @CRLF & @CRLF)
    EndIf
Next

Local $begin = "(("
Local $end = ")))"
Local $loop = 0
For $i = 1 To 2
    While 1
        $loop += 1
        $begin &= "("
        If $loop = $i Then ExitLoop
    WEnd
    $loop = 0
    While 1
        $loop += 1
        $end &= ")"
        If $loop = $i Then ExitLoop
    WEnd
    Local $keyS = "#", $keyE = "@"
    Local $0 = "A))" & $begin & $keyS & "(" & $end & $begin & $begin & "C" & $end & $begin & $end & ")" & $keyE & $end & "(((B"
    Local $A = _StringBetween($0, $keyS, $keyE)
    Local $C = $keyS & $A[0] & $keyE
    Local $B = _StringBetween($0, $begin, $end)
    Local $D = $B[0]
    ConsoleWrite("!-IN: " & $0 & @CRLF)
    ConsoleWrite("String Between: " & $begin & "  <with>  " & $end & @CRLF)
    If $C = $D Then
        ConsoleWrite("+OUT OK: " & $D & @CRLF & @CRLF)
    Else
        ConsoleWrite("- True result: " & $C & @CRLF)
        ConsoleWrite("!     But OUT: " & $D & @CRLF & @CRLF)
    EndIf
Next

Local $begin = "((("
Local $end = ")"
Local $loop = 0
For $i = 1 To 2
    While 1
        $loop += 1
        $begin &= "("
        If $loop = $i Then ExitLoop
    WEnd
    $loop = 0
    While 1
        $loop += 1
        $end &= ")"
        If $loop = $i Then ExitLoop
    WEnd
    Local $keyS = "#", $keyE = "@"
    Local $0 = "A))" & $begin & $keyS & $end & "(" & $begin & $begin & "C" & $end & $begin & $end & ")" & $keyE & $end & "(((B"
    Local $A = _StringBetween($0, $keyS, $keyE)
    Local $C = $keyS & $A[0] & $keyE
    Local $B = _StringBetween($0, $begin, $end)
    Local $D = $B[0]
    ConsoleWrite("!-IN: " & $0 & @CRLF)
    ConsoleWrite("String Between: " & $begin & "  <with>  " & $end & @CRLF)
    If $C = $D Then
        ConsoleWrite("+OUT OK: " & $D & @CRLF & @CRLF)
    Else
        ConsoleWrite("- True result: " & $C & @CRLF)
        ConsoleWrite("!     But OUT: " & $D & @CRLF & @CRLF)
    EndIf
Next

 

Edited by Trong

Regards,
 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I see what you mean, it doesn't output what's in between what you state.

It's a mess, so many characters gave a knot in my brain.

Simplify.

Edited by careca

Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Share this post


Link to post
Share on other sites

I have recently been in need of the _StringBetween function.

 

$read = FileRead("FILENAME.html") ;read file
$Datastring = ('today_live">')
$readamount = _StringBetween($read,$Datastring, "</span>") ;read title from file
MsgBox(0,"",$readamount) ;display first found string

Hope this helps you?

Share this post


Link to post
Share on other sites

I see what you mean

Lucky you...
Personally I DON'T see what the OP means

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thank for the suggestions, but I know the _StringBetween() work.
I need is somethink other, and I has solved this problem!

EG:
 

#include <String.au3>
Local $sString = "<test>C</test><test>B</test><test>A</test>"
ConsoleWrite("+ String: " & $sString & @CRLF)
ConsoleWrite("- Get string between : '<test>' and '</test>'" & @CRLF)
Local $aArray = _StringBetween($sString, "<test>", "</test>", 3)
For $i = 0 To UBound($aArray) - 1
    ConsoleWrite("-OUT: " & $aArray[$i] & @CRLF)
Next
ConsoleWrite("- I do not need it! " & @CRLF)
ConsoleWrite("! I need taken: C</test><test>B</test><test>A" & @CRLF & @CRLF)

Solved:

#include <String.au3>
Local $begin = "(("
Local $end = "))"
Local $loop = 0
For $i = 1 To 2
    While 1
        $loop += 1
        $begin &= "("
        If $loop = $i Then ExitLoop
    WEnd
    $loop = 0
    While 1
        $loop += 1
        $end &= ")"
        If $loop = $i Then ExitLoop
    WEnd
    Local $keyS = "#", $keyE = "@"
    Local $0 = "A))" & $begin & $keyS & "(" & $end & $begin & $begin & "C" & $end & $begin & $end & ")" & $keyE & $end & "(((B"
    Local $A = _StringBetween($0, $keyS, $keyE)
    Local $C = $keyS & $A[0] & $keyE
    Local $B = StringBetween($0, $begin, $end)
    ConsoleWrite("!-IN: " & $0 & @CRLF)
    ConsoleWrite("String Between: " & $begin & "  <with>  " & $end & @CRLF)
    If $C = $B Then
        ConsoleWrite("+OUT OK: " & $B & @CRLF & @CRLF)
    Else
        ConsoleWrite("- True result: " & $C & @CRLF)
        ConsoleWrite("!     But OUT: " & $B & @CRLF & @CRLF)
    EndIf
Next

Local $begin = "(("
Local $end = ")))"
Local $loop = 0
For $i = 1 To 2
    While 1
        $loop += 1
        $begin &= "("
        If $loop = $i Then ExitLoop
    WEnd
    $loop = 0
    While 1
        $loop += 1
        $end &= ")"
        If $loop = $i Then ExitLoop
    WEnd
    Local $keyS = "#", $keyE = "@"
    Local $0 = "A))" & $begin & $keyS & "(" & $end & $begin & $begin & "C" & $end & $begin & $end & ")" & $keyE & $end & "(((B"
    Local $A = _StringBetween($0, $keyS, $keyE)
    Local $C = $keyS & $A[0] & $keyE
    Local $B = StringBetween($0, $begin, $end)
    ConsoleWrite("!-IN: " & $0 & @CRLF)
    ConsoleWrite("String Between: " & $begin & "  <with>  " & $end & @CRLF)
    If $C = $B Then
        ConsoleWrite("+OUT OK: " & $B & @CRLF & @CRLF)
    Else
        ConsoleWrite("- True result: " & $C & @CRLF)
        ConsoleWrite("!     But OUT: " & $B & @CRLF & @CRLF)
    EndIf
Next

Local $begin = "((("
Local $end = ")"
Local $loop = 0
For $i = 1 To 2
    While 1
        $loop += 1
        $begin &= "("
        If $loop = $i Then ExitLoop
    WEnd
    $loop = 0
    While 1
        $loop += 1
        $end &= ")"
        If $loop = $i Then ExitLoop
    WEnd
    Local $keyS = "#", $keyE = "@"
    Local $0 = "A))" & $begin & $keyS & $end & "(" & $begin & $begin & "C" & $end & $begin & $end & ")" & $keyE & $end & "(((B"
    Local $A = _StringBetween($0, $keyS, $keyE)
    Local $C = $keyS & $A[0] & $keyE
    Local $B = StringBetween($0, $begin, $end)
    ConsoleWrite("!-IN: " & $0 & @CRLF)
    ConsoleWrite("String Between: " & $begin & "  <with>  " & $end & @CRLF)
    If $C = $B Then
        ConsoleWrite("+OUT OK: " & $B & @CRLF & @CRLF)
    Else
        ConsoleWrite("- True result: " & $C & @CRLF)
        ConsoleWrite("!     But OUT: " & $B & @CRLF & @CRLF)
    EndIf
Next




; #FUNCTION# ====================================================================================================================
; Name ..........: StringBetween
; Description ...: Find strings between two string delimiters
; Syntax ........: StringBetween($sString, $sStart, $sEnd)
; Parameters ....: $sString         - The string to search.
; Parameters ....: $sStart          - The beginning of the string to find.
;                  $sEnd            - The end of the string to find.
; Return values .: Success: a found string
;                  Failure: sets the @error flag to non-zero.
;                  @error: 1 - No strings found.
; Author ........: Trong
; Related .......: _StringBetween
; Example .......: Yes
; ===============================================================================================================================

Func StringBetween($sString, $sStart, $sEnd)
    $sString = StringReverse(StringTrimLeft($sString, StringInStr($sString, $sStart) + StringLen($sStart) - 1))
    $sString = StringReverse(StringTrimLeft($sString, StringInStr($sString, StringReverse($sEnd)) + StringLen($sEnd) - 1))
    Return SetError(StringLen($sString) = "", 0, $sString)
EndFunc   ;==>StringBetween

 

Edited by Trong

Regards,
 

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