iCode Posted July 7, 2011 Share Posted July 7, 2011 (edited) hii'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-4433this matches...787-888-7788344-343-4433the 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 July 7, 2011 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) Link to comment Share on other sites More sharing options...
enaiman Posted July 7, 2011 Share Posted July 7, 2011 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 :) Link to comment Share on other sites More sharing options...
iCode Posted July 7, 2011 Author Share Posted July 7, 2011 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) Link to comment Share on other sites More sharing options...
enaiman Posted July 7, 2011 Share Posted July 7, 2011 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 :) Link to comment Share on other sites More sharing options...
jchd Posted July 7, 2011 Share Posted July 7, 2011 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 numIf 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 hereRegExp tutorial: enough to get startedPCRE 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) Link to comment Share on other sites More sharing options...
iCode Posted July 7, 2011 Author Share Posted July 7, 2011 thanks very much jchd - that is exactly what i needed to knowi knew i was "using" the space, but wasn't sure why or how to get around itbrilliant! 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) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now