Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Simple Regex

by Juerd (Abbot)
on Apr 08, 2002 at 17:12 UTC ( [id://157485]=note: print w/replies, xml ) Need Help??


in reply to Simple Regex

You love "5", don't you? :)

I'd probably filter all non-alphanumerics, and then split on any string of letters.

while (<DATA>) { chomp; tr/A-Za-z0-9//cd; my ($number, $extension, $overflow) = split /[A-Za-z]+/; if ($overflow) { warn "Don't know how to handle number '$_'.\n"; next; } print "Number: $number"; print ", extension: $extension" if defined $extension and length $ +extension; print "\n"; } __DATA__ (555) 555-5555 555.555.5555 555-555-5555 (555)555.5555 (555) 555-5555 x.555 555.555.5555 Ext. 555 555-555-5555 ext.555

U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk

Replies are listed 'Best First'.
Re2: Simple Regex
by dragonchild (Archbishop) on Apr 08, 2002 at 17:16 UTC
    Good solution, cutting to the chase.

    However, you still have to worry about malformations, such as phone numbers that aren't 7 or 10 digits. Often times, people will want to have the area code somewhere else, too. *shrugs*

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      However, you still have to worry about malformations, such as phone numbers that aren't 7 or 10 digits.

      So every database has numbers only within a single country? Not like any database I've ever used. I even thought about not filtering out leading plusses, but didn't do so, because I think this was homework anyway - and there should still be a challenge.

      As for 7 or 10 digits, I had no idea about how other countries have their telephone numbers, and think I should not guess.

      The fix: check length.

      U28geW91IGNhbiBhbGwgcm90MTMgY
      W5kIHBhY2soKS4gQnV0IGRvIHlvdS
      ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
      geW91IHNlZSBpdD8gIC0tIEp1ZXJk
      

        Juerd you're showing your europeanism... :-)

        So every database has numbers only within a single country? Not like any database I've ever used.

        Dont forget that ALL of North America (and the Bahamas) uses the same country code (1) and even more unlike Europe they use a standardized number format (3-3-4) _and_ they use extensions (which Germany for example does not).

        I even thought about not filtering out leading plusses

        Most North Americans dont know what the plus would mean anyway. (heh)

        The fix: check length.

        Unfortunately that doesnt work, the extension can be practically any number you like...

        Now you know how I felt when I first got to Europe... Confused. (And lets not even talk about different dial tones and signals, I still get confused sometimes if it ringing or not....)

        :-)

        Yves / DeMerphq
        ---
        Writing a good benchmark isnt as easy as it might look.

Re x 2: Simple Regex
by Sweeper (Pilgrim) on Apr 08, 2002 at 20:09 UTC
    You love "5", don't you? :)

    The 555 area code is a well-known area code that appears only in Hollywood movies and other fiction stuff. There is even a web page about that: http://home.earthlink.net/~mthyen/

    So, if you want to "sanitize" a piece of code containing phone numbers (for privacy reasons and to fight spam... er telemarketing), you replace these by phone numbers from the 555-area.

    Yet, maybe Sevrin could have said 555-1234-5678 or 555-2002-0408 :-)

    update

    I have forgotten the following example. In Mac Perl, Power and Ease (published by Prime Time Freeware http://www.ptf.com/), both authors (Vicki Brown and Chris Nandor) give their phone numbers:

    $phone{"Vicki"} = "555-1234";
    $phone{"Chris"} = "555-4321"; 
    
    You can read it on-line at http://ptf.com/macperl/ptf_book/r/MP/120.SS.html#03

    Another update. Sevrin, may be you could look at some of the modules you get in http://search.cpan.org/search?mode=module&query=phone

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://157485]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-23 07:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found