Jump to content
Sign in to follow this  
benjbong

Question about stringregexp

Recommended Posts

benjbong

I'm having trouble understanding stringregexp. I've experimented and read the help file over and over but nothing seems to be clicking with me. My end goal is to be verify whether or not a string is in the format of hrs:mins:secs like 1:15:01

I would think \d:\d\d:\d\d would work?

Share this post


Link to post
Share on other sites
benjbong

so if i have the script

$string = "0:00:0a"
if not StringRegExp($string, 'd:dd:dd') Then
msgbox(0,"","invalid time")
EndIf

it works. But if i change this line to:

$string = "a0:00:00"

it doesn't say it's invalid. How to i make sure entire string is in the format of 0-9:0-90-9:0-90-9?

Edited by benjbong

Share this post


Link to post
Share on other sites
kylomas

benjbong,

Nor should it because the regex is matching on your pattern in both instances. To make matching start at the beginning of the line you need to use the "(?m)" specification first followed by the start of line character, "^". You pattern might look like "(?m)^d:dd:dd".

I'm just learning regex so you may need to experiment.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
jchd

You need to anchor your pattern. StringRegExp($string, '^d:dd:dd$')

If you don't, the regexp engine will find a matching part in your subject string, abcd7:15:23efgh would match.

Also, are you sure you allow for 8:99:99 to match?

@kylomas,

The multiline option isn't required, at least from what the OP shows.

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
kylomas

@jchd,

Thanks


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
jchd

You're always welcome.

Should the OP have to deal with $s = '1:23:45' & @CRLF & '2:34:56' & @CRLF & '3:45:67'

then, yes, the pattern would need to be '(?m)^d:dd:dd$'

But still my question remains open: are seconds and minutes > 59 to be allowed?


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
kylomas

@jchd,

Given the string "12:32:31" pattern "^dd:dd:dd" matches, but pattern "^dd:dd:dd$" does not. I am using a string regexp tester I found on the forum to test this. I deon't understand the use of "$", please explain.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
benjbong

You need to anchor your pattern. StringRegExp($string, '^d:dd:dd$')

If you don't, the regexp engine will find a matching part in your subject string, abcd7:15:23efgh would match.

Also, are you sure you allow for 8:99:99 to match?

@kylomas,

The multiline option isn't required, at least from what the OP shows.

That works, thanks. So the ^ indicates the start of the string and the $ indicates the end? But you're right, i would need it to be 0-60 in the mins and secs portion.

would this work:

StringRegExp($string, '^d:[0-5]d:[0-5]d$')
Edited by benjbong

Share this post


Link to post
Share on other sites
benjbong

That works, thanks. So the ^ indicates the start of the string and the $ indicates the end? But you're right, i would need it to be 0-60 in the mins and secs portion.

would this work:

StringRegExp($string, '^d:[0-5]d:[0-5]d$')

I meant to say 0-59. The expression I put up there works. Thanks you guys, this is making a lot more sense now. Is there a better resource than the help file to better learn expressions?

Share this post


Link to post
Share on other sites
benjbong

@jchd,

Given the string "12:32:31" pattern "^dd:dd:dd" matches, but pattern "^dd:dd:dd$" does not. I am using a string regexp tester I found on the forum to test this. I deon't understand the use of "$", please explain.

kylomas

It looks like you're missing the in front of the second d - you have dd instead of dd

Share this post


Link to post
Share on other sites
kylomas

benjbong,

Good catch,

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
jchd

Correct about ^ and $. They (generally) match start and end of subject string, so your subject can't show anything before or after the pattern. In multiline mode (the (?m) discussed) they match at line boundaries as well, so the pattern will match a line in my example above (where you just now rightfully found a typo, thanks [it's a bit late for me, 2AM]).

Edit: I thought it was in mine!

Yes your [0-5]d subpattern is correct for minutes and seconds.

I'll post a very good regexp reference shortly, stay tuned.

Here it is: http://www.asiteaboutnothing.net/regexp/ HIGHLY recommended!

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
benjbong

Correct about ^ and $. They (generally) match start and end of subject string, so your subject can't show anything before or after the pattern. In multiline mode (the (?m) discussed) they match at line boundaries as well, so the pattern will match a line in my example above (where you just now rightfully found a typo, thanks [it's a bit late for me, 2AM]).

Edit: I thought it was in mine!

Yes your [0-5]d subpattern is correct for minutes and seconds.

I'll post a very good regexp reference shortly, stay tuned.

Brilliant, cheers.

Share this post


Link to post
Share on other sites
jchd

I posted the site (see above).


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  

×