Sign in to follow this  
Followers 0
FinalVersion

Need a RegExp

19 posts in this topic

Basically I need a regular expression that follows, a-z (alphabetic characters), whitespace (any old whitespace), 0-9 (digits). So "abc 123" would be valid, "abc123" wouldn't be valid, nor would 'abc dfg 123",or "abd 123 456".

I tried, it sort of works, but not the extent that I want.

$sTest = "abc 123"

$iResult = StringRegExp($sTest, "\w\h\d")

If $iResult = 1 Then
    MsgBox(0, "", "1")
Else
    MsgBox(0, "", "Non 1")
EndIf

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

"([a-z]+\h[0-9]+)"

Edit: Remember that when you have to match something more tham once you have to use a quantifier like I did with the + which means match one or more times. There are also going to be times when you have to invert greedyness. To match a given number of times use {<number of times to match} as in {3} for match 3 times. {3,} would mean to match at least 3 times and {3,6} Means match at least 3 times and not more than 6 times.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Try:

Local $sTest[15] = [ _
    "", _
    "a", _
    "abc", _
    "abc    ", _
    " ", _
    "1", _
    " 123", _
    "a 1", _
    "abc    123", _
    " abc 123", _
    "abc123", _
    "abc 123    ", _
    "abc 1 2 3", _
    "a b c 123", _
    "abc 123a" _
]

For $i = 0 To UBound($sTest) - 1
    ConsoleWrite(StringFormat('%-15s %u\n', '>' & $sTest[$i] & '<', StringRegExp($sTest[$i], "\A[a-z]+\s+\d+\z")))
Next

The pattern matches if and only if, at the begining of the string it finds one or more lowercase english letter (a-z) followed by one or more whitespace followed by one or more decimal digit (0-9) until the end of the string.


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

@George,

Beware that your pattern isn't anchored, so it will match

"abc def 123 456"

"123 a b 4 xyz 000"

"********************* 123 aaaaaaa 7$$$$$$$$$$$$ "


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

#5 ·  Posted (edited)

I think it depends more on what he's trying to do.

#include<array.au3>
$sStr = "a abc 123 abc 456 avf" & @CRLF
$sStr &= "cde 5678 and this is the last test hjk 876" & @CRLF
$sStr &= "Well maybe one more xyz      789"

$aArray = StringRegExp($sStr, "[a-z]+\h+[0-9]+", 3)
_ArrayDisplay($aArray)

Edit: I should add that primarily he was on the right track but forgot the quantifiers.

Also I deplore complexity for the sake of complexity. We could have used a few \b in there too but for what good reason?

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Sure, but our Op was talking about Pass/NoPass validation, not capturing several instances within a flow, so...

At least that gives him a choice and various possibilities.

Phew, too late --> bed for me!

Bye all.

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

Sure, but our Op was talking about Pass/NoPass validation, not capturing several instances within a flow, so...

At least that gives him a choice and various possibilities.

Phew, too late --> bed for me!

Bye all.

You're right but now he has the options and a couple of lessons

As for added complexity, why the h**l did I use [0-9]+ when all it needed was \d+ Must have been for the sake of adding complexity.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

"([a-z]+\h[0-9]+)"

Edit: Remember that when you have to match something more tham once you have to use a quantifier like I did with the + which means match one or more times. There are also going to be times when you have to invert greedyness. To match a given number of times use {<number of times to match} as in {3} for match 3 times. {3,} would mean to match at least 3 times and {3,6} Means match at least 3 times and not more than 6 times.

You only need to invert greedyness to stop a match where the previous step would keep going such as if you had ABCDEFGH

.*D

would match

ABCDEFGH

but .*?D

or .{3}D

would only match

ABCDEFGH

In his case [a-z] would not match the white space so "([a-z]+\h[0-9]+)" is fine, except \h should be \s because \h is nothing I believe, and unless there are upper case. Then you need either (?i) at the beginning or [a-zA-Z] instead.

Also just adding quantifiers to his and changing h to s works, (\w+\s+\d+)

That is IF you want it to match across lines because \s includes \r and \n so

abc

123

will match.

If you want only spaces and tab white space then (\w+[\t ]+\d+) works instead

Edited by ShawnW

Share this post


Link to post
Share on other sites

\h = horizontal white space, \v = vertical whitespace and \s can be either.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

\h = horizontal white space, \v = vertical whitespace and \s can be either.

Interesting, I've used regex's for years and never known of those. RegexBuddy didn't either but sure enough its in the autoit manual, which I didn't read because I already know regular expressions. maybe it's not standard in all regular expression engines. Anyway, good tip!

Share this post


Link to post
Share on other sites

You only need to invert greedyness to stop a match where the previous step would keep going such as if you had ABCDEFGH

.*D

would match

ABCDEFGH

[nitpickmode]

.*D? would but Not so for .*D as it will match ABCDEFGH only since the D isn't optional.

[/nitpickmode]

I believe \h has not been around for long time in PCRE.


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

#12 ·  Posted (edited)

[nitpickmode]

.*D? would but Not so for .*D as it will match ABCDEFGH only since the D isn't optional.

[/nitpickmode]

I believe \h has not been around for long time in PCRE.

Your right I'm an idiot I was thinking of an example where I had multiple matches on the same line one time like

ABCDEFGH ABCDEFGH

where you would need

(?:(.*?)D.*? )
if you needed everything before D in each match captured. Don't know what I was thinking when I posted that duh.

I used code there because I couldn't figure out how to disable emoticons, how do you do that?

Edited by ShawnW

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Interesting, I've used regex's for years and never known of those. RegexBuddy didn't either but sure enough its in the autoit manual, which I didn't read because I already know regular expressions. maybe it's not standard in all regular expression engines. Anyway, good tip!

That's one of the major problems with using regexp. There are so many engines out there and although they will all have some things in common there are slight differences in each one. RegEx Buddy is not PCRE explicit so the fact that it's not recognized is no surprise. I can't remember now if Expresso accepts it or not but I think it does.

For AutoIt users just starting with SREs, the best answer is to use an SRE tester which is explicit to AutoIt.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

@SchaunW No you ain't idiot and you know it. Everybody makes dumb mistakes just like you and me without being a total jerk. I didn't want to point _you_ wrong, but I felt leaving out an error in this topic could trap some day some new user having found the search engine ;-) <-- is that turned into a smiley?

@GEOsoft You're plain right but I don't see in slight dialect differences such a hassle, at least it isn't specific to regexps: when you've gained some experience with basic regexps you fairly quickly understand how to circumvent discrepencies between engines. Yet differences in tokens, or worst, global behavior can be misleading to newcomers.

Another way to view this: look at any open source piece of C software of significant complexity ported to a few OSes and you'll find a number of #ifdef and significant differences in basic types and the like. Even with standard C code, the standards have to be "smoothen" in practice by tons of conditionals and macros for things to work with different compilers (all claiming to compile standard C) or distinct OSes (all claiming to offer standard libraries). Our IT world is a bit of a mess in almost all aspects, so it isn't very surprising that regexp are from this point of view no exception, particularly whn no official standard is in force to put things mostly straight.


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

.... Our IT world is a bit of a mess in almost all aspects, ....

Also, I suspect IT has the best of almost all worlds.

Share this post


Link to post
Share on other sites

"War" is an inappropriate term. You know, regexps are like both a hot lover or a bottle of nitroglycerin. They can be unbelievably powerful and give you much joy but demand in return real care to manipulate them.

Since we all make gross or subttle mistakes at times (and I can win at this game), it's better when someone looks over your shoulder and notices before the whole thing blows up in the face of some innocent user saying himself "Here's a nice example I can use for doing my conver..." BOOM !


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

Don't you just love those LITTLE mistakes? The typos like /s instead of \s or (/s) instead of (?s)


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Probable cosmic rays messing with the keyboard µC, _I_ would never make such blattant mistake :(

I used to like . vs \. to some extent: simple and efficient as sometimes you would dissect the pattern 10 times and don't get it. But I haven't made it recently, maybe my fingers now cross the right way.


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