Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Actually, your regex, while good, has a slight problem. In a character class (something like [a-zA-Z]), the target text is filtered against each individual character in the class. No bar '|' for alternation is necessary. Your class actually is testing for a dash, a bar, and a space, rather than alternating between dash and space. Here's this code slightly cleaned up:
#!/usr/local/bin/perl -w use strict; while (<DATA>) { s /[\n\r]//g; print "Testing with $_, result is "; m/(?:1[- ]?)? # Testing for an leading 1 and dash or space behi +nd it \(? # Optional parentheses around the area code (\d{3}) # Area code \)? # Close optional parentheses around area code [- ]? # Optional dash or space after area code (\d{3}) # Exchange [- ]? # Optional dash or space after exchange (\d{4}) # Line /x; # /x modifier allows whitespace in a regex my $areacode = $1; my $exchange = $2; my $line = $3; print "($areacode) $exchange-$line\n"; } __DATA__ 212-555-1212 (212)555-1213 1-(212)-555-1214 1-212-555-1215 212 555 1216 (212) 555 1217 1 (212) 555 1218 1 212 555 1219 12125551210
A couple of notes on the code: when single character alternates are allowed (that's not quite the same thing as single byte alternates), a character class is much faster than alternation. In other words, we're using [- ] rather than (-| ).

I also switched the first set of parentheses from (1[- ]?) to (?:1[- ]?). The (?:someregex) construct allows for grouping without backreferencing to a $somenumber variable. This is more efficient than straight parentheses for grouping and should be used, when possible.


In reply to RE: Answer: How do I break down a phone number and rebuild it? by Ovid
in thread How do I parse a telephone number? by mofo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found