Sign in to follow this  
Followers 0
Steveiwonder

RegExp - Is this correct?

10 posts in this topic

#1 ·  Posted (edited)

Hey all,

Didn't want to ask for someone to just do what i needed but can someone atleast confirm if i'm right/wrong. I'd like to point out RegExp's blow my brain!

Firstly "$id" must be a combination of exactly 10 random numbers. Would the below work?

(Yes i know how simple it is for you guru's, i just simply fail at Regular Expressions)

$id = "2003714598"

If StringRegExp($id, '\b[0-9]{10}\b') Then
    MsgBox(0, "Match", $id & " Found")
Else
    MsgBox(0, "No Match", $id & " Not Found")
EndIf

And also this one..

Has to match the start of the URL and then 7 random digits then the end of the URL.

$url = "https://somerandomwebsite.ams.uk.com/details.php?i=1245795&t=d"

If StringRegExp($url,"[https://somerandomwebsite.ams.uk.com/details.php?i=][0-9]{7}[&t=d]") Then
    MsgBox(0, "Match", $url & " Found")
Else
    MsgBox(0, "No Match", $url & " Not Found")
EndIf

I would just like to know weather i have mastered (as far as my brain will allow) the art of Regular Expressions or can it be done another way.

Thanks

Steve

Edited by Steveiwonder

They call me MrRegExpMan

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Your first RegEx pattern appears to be correct if you want to match a word that consists of exactly 10 numbers.

There is an error with your second RegEx pattern. You should remove the [] from around the first part of the URL. As it is it will only match one of the characters between the square brackets and I assume that you want to match the whole of the first part of the URL. Also the last bit should not be enclosed by [] 

 

 

Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Let me expand a bit on the good answer already provided.

You may also use \d in place of [0-9] to match a (decimal) digit. So your first reg. expression can be '\b\d{10}\b' equivalently. This is only a handy shorthand.

\b matches a "word boundary" so the regexp above means "I match if I find a sequence of 10 decimal digits surrounded by whitespaces or begin/end of string"

Hence '\b\d{10}\b' will hapily match 'gfhuo nio rzjlik hre 0123456789 blbdn lk'

But

If you want to enforce the rule that your input "must be a combination of exactly 10 random numbers" then the first regexp isn't exactly what you need, as the example above shows.

Make your regexp '\A\d{10}\z', meaning "I match if the string contains exclusively a sequence of 10 decimal digits." This one leaves no room for chance.

You see on this simple example that you can and probably should match precisely what you intent, nothing less but nothing more.

For instance, what would you choose: "I say that the code is 0123456789 ... OMG, I made a horrible mistake: the actual code is 9876543210 forgive me dear fellow!"

The first regexp will happily pick 0123456789 but the second one will bark, giving you the opportunity to catch the error.

In short: regexp are very powerful and offer means to express precise conditions. When you use them routinely choose the right balance between being pointlessly too strict and lazily too permissive. Anyway, regexp will bring you joy and frustration, just like a real-life partner.

EDIT: a couple of typos, my bad.[really bad, yet another: [0-9] = \d not \b of course. Thanks Trancexx!]

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

I think i may be confusing myself...

Without the [] it doesnt find match, but with it does.

Local $oLinks[6]
$oLinks[0] = "wadad"
$oLinks[1] = "awdawd"
$oLinks[2] = "354345$%@$%$:%@£$5"
$oLinks[3] = "fewdawda.com wdwad23423 "
$oLinks[4] = "https://randomsite.com/roooar/details.php?i=7548924&t=d"
$oLinks[5] = "wddadd33444444322&&*&*£$%£"

$pattern = "https://randomsite.com/roooar/details.php?i=[0-9]+&t=d"
;$pattern = "[https://randomsite.com/roooar/details.php?i=][0-9]+[&t=d]"

For $oLink in $oLinks
        
        If StringRegExp($oLink, $pattern) then 
            ;_IENavigate($oIE, $oLink)
            Msgbox(0,"Match", "Match Found")
            ExitLoop            
        EndIf       
Next

Thanks for both replies.

Steve


They call me MrRegExpMan

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I think i may be confusing myself...

Without the [] it doesnt find match, but with it does.

Local $oLinks[6]
$oLinks[0] = "wadad"
$oLinks[1] = "awdawd"
$oLinks[2] = "354345$%@$%$:%@£$5"
$oLinks[3] = "fewdawda.com wdwad23423 "
$oLinks[4] = "https://randomsite.com/roooar/details.php?i=7548924&t=d"
$oLinks[5] = "wddadd33444444322&&*&*£$%£"

$pattern = "https://randomsite.com/roooar/details.php?i=[0-9]+&t=d"
;$pattern = "[https://randomsite.com/roooar/details.php?i=][0-9]+[&t=d]"

For $oLink in $oLinks
        
        If StringRegExp($oLink, $pattern) then 
            ;_IENavigate($oIE, $oLink)
            Msgbox(0,"Match", "Match Found")
            ExitLoop            
        EndIf       
Next

Thanks for both replies.

Steve

I worked out what im doing wrong i think...

I needed to escape some special characters? So the pattern i now have is this..

$pattern = "https\://randomsite\.com/roooar/details\.php\?i=[0-9]+&t=d"

Am i on the right track?

Edited by Steveiwonder

They call me MrRegExpMan

Share this post


Link to post
Share on other sites

Steveiwonder,

You have it. :D

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Woop! Now anyone who needs help with RegExp's just throw them my way i have mastered the art!

On a serious note, thanks everyone :D

Edited by Steveiwonder

They call me MrRegExpMan

Share this post


Link to post
Share on other sites

Steveiwonder,

Woop! Now anyone who needs help with RegExp's just throw them my way i have mastered the art!

:D

Dream on........

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

...

EDIT: a couple of typos, my bad.

You changed a but you forgot to change b.

Nice post, btw.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

You changed a but you forgot to change b.

Oops, Good catch. That's age + no sleep.

It was a good opportunity to have the OP's -and possibly some readers'- curiosity stimulated so that they dig further into regexp.

In a while, OP and other will routinely write working "serious" regexps, just like this one, which will match any form of valid email address:

This is page 316 of Jefferey' book Mastering Regular Expressions ed.1 (I didn't find it in text form on the Internet)


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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