Jump to content
Sign in to follow this  
iCode

PERLRE RegExp help

Recommended Posts

iCode

hi

i'm trying to match phone numbers (not that that matters) with a RegEx and having trouble trying to figure this out, even after reading several documents and posts here.

this is the closest i have gotten:

(?:^|\s)((?:1 |1-|\+1 )?\(?\d{3}\)?[ -]\d{3}-\d{4})(?:$|\s)

here's a sample test string:

555-5642-1212 787-888-7788 799-000-0000 344-343-4433

this matches...

787-888-7788

344-343-4433

the problem is i'm not matching the middle number and i know it's because of the outside non-capturing groups

(?:^|\s)and (?:$|\s), specifically the \s. i can understand why this is not working, but i don't know how to get around it since i need the space before and after each phone num.

i'm stuck :)

Edited by iCode

FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Share this post


Link to post
Share on other sites
enaiman

IMO it can be done much easier with a simpler expression:

#include<array.au3>
$str = "555-5642-1212 787-888-7788 799-000-0000 344-343-4433"
$arr = StringRegExp($str, "\d{3}-\d{3}-\d{4}", 3)
_ArrayDisplay($arr)

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites
iCode

sorry, i neglected to mention that i need to pull the numbers from a file where they could be mixed with whatever else, such as a web page for example. so in order to avoid what may be non-phone numbers ('abc222-333-4444' or '333-444-5555-333-444-5555'), i need to look for a space both before and after the number.

the regex i'm using will match stuff like...

+1 222-333-4444

1 222-333-4444

1-222-333-4444

which works fine until you run multiple numbers together on the same line...

222-333-4444 333-444-5555 444-555-6666

in which case it won't match the middle num


FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Share this post


Link to post
Share on other sites
enaiman

Why don't you StringSplit for " " your content and search every member after that for matches?

You'll need some good logic involved to detect such cases (+1 222-333-4444, 1 222-333-4444 ...) but it can be done.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites
jchd

i need to look for a space both before and after the number.

...

222-333-4444 333-444-5555 444-555-6666

in which case it won't match the middle num

If you use a non-capturing group to match a space after a phone number, then this space is no more "available" for the space you require ahead of a patternin themiddle of a string.

You need to use look-behind and look-ahead assertions resp. which do not bump the index in the subject line and don't capture either.

(?<=^|\s)((?:1 |1-|\+1 )?\(?\d{3}\)?[ -]\d{3}-\d{4})(?=$|\s)

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
iCode

thanks very much jchd - that is exactly what i needed to know

i knew i was "using" the space, but wasn't sure why or how to get around it

brilliant!


FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

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  

×