Jump to content

Help on StringRegEx


Recommended Posts

Is this coding right? Trying out StringRegExp first time and using newest AutoIT build...I wanted StringRegExp to extract numbers between &pid= and &page=, up to 7 digits number but I ran a test on the code below and I got blank result in msgbox, whats wrong here?

CODE
Dim $URL, $PID

$URL = "http://anywhere.com/view?f=catagory&pid=99453&page=1&order=down"

$PID = StringRegExp($URL, "(?:&pid=)([0-9]{1,7})(?:&page=)", 1)

MsgBox(0,"Result", $PID)

Thanks.

Link to comment
Share on other sites

StringRegExp drives me crazy. I wish there were better examples/explanations in the help file. But one thing I can tell you, you've set option 1, which returns an array.

edit - Think I found the solution, though...

$URL = "http://anywhere.com/view?f=catagory&pid=99453&page=1&order=down"
$PID = StringRegExp($URL, 'pid=(.*?)\&page', 1, 1)

If IsArray($PID) Then
    For $i = 0 To UBound($PID) - 1
        MsgBox(0, '', $PID[$i])
    Next
EndIf
Edited by xcal
Link to comment
Share on other sites

You could also do it like this (The array point is correct by @xcal)

#include <array.au3>
Dim $URL, $PID

$URL = "http://anywhere.com/view?f=catagory&pid=99453&page=1&order=down"
$PID = StringRegExp($URL, "&pid=(\d+)&page=", 3) ;Note 3 to get a global search
_ArrayDisplay($PID, "test")
Link to comment
Share on other sites

This is how I would have set it up, though yours seems to be fine. Just set it to search through in a loop.

Dim $URL, $PID

Dim $linePattern

$linePattern = '(?i)(?:pid=)(\d*)'

$URL = "http://anywhere.com/view?f=catagory&pid=99453&page=1&order=down"
$PID = StringRegExp($URL, $linePattern, 3)
For $i = 0 To UBound($PID) - 1 Step 1
    MsgBox(0,"Result", $PID[$i])
Next

I hope that helps. xcal has given same basic answer. Loop is what you were missing.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

This is how I would have set it up, though yours seems to be fine. Just set it to search through in a loop.

Dim $URL, $PID

Dim $linePattern

$linePattern = '(?i)(?:pid=)(\d*)'

$URL = "http://anywhere.com/view?f=catagory&pid=99453&page=1&order=down"
$PID = StringRegExp($URL, $linePattern, 3)
For $i = 0 To UBound($PID) - 1 Step 1
    MsgBox(0,"Result", $PID[$i])
Next

I hope that helps. xcal has given same basic answer. Loop is what you were missing.

JS

I didnt know StringRegExp requires a loop and an array to get that simple number extraction. Surely there is must be another way to StringRegExp without all that?

I could use $PID = Number(StringMid($URL, 41, 7)) as one liner...but I thought StringRegExp would be more efficient.

Link to comment
Share on other sites

I didnt know StringRegExp requires a loop and an array to get that simple number extraction. Surely there is must be another way to StringRegExp without all that?

I could use $PID = Number(StringMid($URL, 41, 7)) as one liner...but I thought StringRegExp would be more efficient.

StringRegExp is to search for content that may not be in the same place all the time. It will be more efficient in more complex situations than some where you know the exact location will always be.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

After looking at String Regular expression in AutoIT's help file on SRE example #6 few minutes ago and I noticed that I have missed a small code:

MsgBox(0, "SRE Example 6 Result", $asResult[0])

So I ran a test on this code:

Dim $URL, $PID

$URL = "http://anywhere.com/view?f=catagory&pid=99453&page=1&order=down"
$PID = StringRegExp($URL, "(?:&pid=)([0-9]{1,7})(?:&page=)", 1)
MsgBox(0,"Result", $PID[0])

and it works. Is that ok for me to use this code?

Link to comment
Share on other sites

After looking at String Regular expression in AutoIT's help file on SRE example #6 few minutes ago and I noticed that I have missed a small code:

MsgBox(0, "SRE Example 6 Result", $asResult[0])

So I ran a test on this code:

Dim $URL, $PID

$URL = "http://anywhere.com/view?f=catagory&pid=99453&page=1&order=down"
$PID = StringRegExp($URL, "(?:&pid=)([0-9]{1,7})(?:&page=)", 1)
MsgBox(0,"Result", $PID[0])

and it works. Is that ok for me to use this code?

Yes that is okay. The loop is only needed if there will be more than one match.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

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...