Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] New, improved phone # Parser - Try it - Comments?

From: doug::vos::eds::com at: 5-Dec-2001 16:59

I wrote a new phone-rule today for parsing a great variety of phone numbers. The script is attached below. This one is improved over the phone parser shown in rebol parser examples. Looking for your comments and tests on things it does not catch. Here are a few tests I have run already...
>> parse/all "+USA 01-312-999-1212" phone-rule
== true
>> parse/all " 01-312-999-1212" phone-rule
== true
>> parse/all "999-1212" phone-rule
== true
>> parse/all "312.999.1212" phone-rule
== true
>> parse/all "999.1212" phone-rule
== true
>> parse/all "999-1212" phone-rule
== true
>> parse/all "(312)-999.1212" phone-rule
== true
>> parse/all "(312)999.1212" phone-rule
== true
>> parse/all "+CANADA 001-312 999-1212" phone-rule
== true
>> parse/all "+CANADA 001-312 9991212" phone-rule
== true
>> parse/all "+CANADA 001-(312)9991212" phone-rule
== true
>> parse/all "+CANADA 001-(312)999-1212" phone-rule
== true
>>>> parse/all "+CANADA 011-(312)999-1212" phone-rule
== true
>> parse/all "+CANADA +011-(312)999-1212" phone-rule
== true
>> parse/all "9991212" phone-rule
== true parse/all "212" phone-rule == false
>> parse/all "5212" phone-rule
== true The next thing I want to add is the ability to break out the parts of the phone number and store them in proper category. Need to append the data to blocks as I parse. Don't know how to do that, yet. This is what I want as output, eventually.. Example: +001-312-677-9999 or +USA 1 (312)677-9999 or 312.677.9999 or 677.9999 dial-one area-code lata-code extension ======== ========= ========= ========= 001 312 677 9999 output should be ["001" "312" "677" "9999"] or [none none "677" "9999"] or something like that. ;----------------------------------------------------------------------- REBOL [] dial-one: charset "01" digit: charset "0123456789" alpha-pn: charset ["+-.() " #"A" - #"Z" #"a" - #"z"] other-ps: charset "+-.() " area-c: [3 digit] lata-c: [3 digit] ph-suffix: [4 digit] phone-rule: [ some [ any alpha-pn ; +USA 312-999-999 any dial-one ; 01-313-999-999 any other-ps ; any area-c 0 3 other-ps ; 1(313)999-9999 lata-c 0 3 other-ps ph-suffix | any alpha-pn any other-ps lata-c 0 3 other-ps ph-suffix | ; 281-1999,"281 1999",2819999 0 3 other-ps ph-suffix ; "-1999","1999" ] to end ] ;--------------------------------------------------------------------------- - ; Insert your phone list here: ; phones: [] ; ;--------------------------------------------------------------------------- - foreach ph phones [ phone: parse/all ph phone-rule if not phone [print ph] ] ; Thanks! ; Doug Vos