Jump to content

Quick question about a regular expression


Recommended Posts

$string = "list1:id=1,id=2,id=3,"
$pattern = "list1\:(id\=(\d+)\,)*"

$result = StringRegExp( $string, $pattern , 3 )
If @error Or Not IsArray($result) Then
    ConsoleWrite( "Error > " & @error & @LF & "Extend: " & @extended & @LF)
    Exit
EndIf

$item = ""
For $item In $result
    ConsoleWrite( "--"&$item&@LF)
Next

Why my regular expression returns only one match? If I use * at the end, I get the first match (id=1). if I use +, I get the last match only (id=3)

+>13:41:17 AU3Check ended.rc:0
>Running:(3.2.6.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Scripts\BG MC\Lib\temp.au3" 
--id=1
--1
+>13:41:18 AutoIT3.exe ended.rc:0
+>13:41:19 AutoIt3Wrapper Finished

[edit]Typos and fixed pattern

Thanks

RK

Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

This outputs:

12

13

14

$string = "list1:id=12,id=13,id=14"
$pattern = "id\=(\d*)"

$result = StringRegExp( $string, $pattern ,3)
If @error Or Not IsArray($result) Then
    ConsoleWrite( "Error > " & @error & @LF & "Extend: " & @extended & @LF)
    Exit
EndIf

For $X = 0 To Ubound($result) - 1
    ConsoleWrite( "--"& $result[$X] &@LF)
Next

And this...

$pattern = "id\=(\d+)"

This relies on the trailing comma being in the string:

$pattern = "id\=(\d*)\,"

Link to comment
Share on other sites

Well, this gets you all three: $pattern = "id\=\d+"

>Running:(3.2.10.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Temp\Test1.au3"    
--id=1
--id=2
--id=3
+>14:29:19 AutoIT3.exe ended.rc:0

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks guys, but the string i'm looking for is in a web page that has this:

list1:id=12,id=13,id=14

list2:id=12,id=22,id=14"

list3:id=10,id=11,id=14"

list4:id=01,id=13,id=10000"

So I must put the string "list1" in my pattern to match that specific list... but when I do that, I get only one match :) I need all of them

Thanks

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

Thanks guys, but the string i'm looking for is in a web page that has this:

list1:id=12,id=13,id=14

list2:id=12,id=22,id=14"

list3:id=10,id=11,id=14"

list4:id=01,id=13,id=10000"

So I must put the string "list1" in my pattern to match that specific list... but when I do that, I get only one match :P I need all of them

Thanks

I know there's a way to combine those two operation into one pattern, but I didn't find it yet. This does it in two passes:

$sString = "list1:id=12,id=13,id=14" & @CRLF & "list2:id=12,id=22,id=14" & @CRLF & _
        "list3:id=10,id=11,id=14" & @CRLF & "list4:id=01,id=13,id=10000"

$sPattern = "list1\:.*\R" ; finds from "list1:" to end of line
$avResult = StringRegExp($sString, $sPattern, 3)
If @error Or Not IsArray($avResult) Then
    ConsoleWrite("Error1 > " & @error & @LF & "Extend: " & @extended & @LF)
    Exit
Else
    $sPattern = "id\=\d+" ; finds "id=n"
    $avResult = StringRegExp($avResult[0], "id\=\d+", 3)
    If @error Or Not IsArray($avResult) Then
        ConsoleWrite("Error2 > " & @error & @LF & "Extend: " & @extended & @LF)
        Exit
    EndIf
    For $sItem In $avResult
        ConsoleWrite("--" & $sItem & @LF)
    Next
EndIf

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

$sPattern = "(?<=list1\:)id\=\d+"

I don't have time to check this, but I think it'll work. Should find "list1:" and then all "id=" and associated numbers.

Result:

>Running:(3.2.10.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Temp\Test_1.au3"   
--id=12
+>21:27:32 AutoIT3.exe ended.rc:0

...because "id=12" is the only one immediately preceded by "input1:".

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...