Another way:

c:\@Work\Perl\monks>perl -wMstrict -le "for my $s (qw(15556667777 3334445555 999999999 88888888 121212121 +212)) { printf qq{'$s' -> }; my ($npa, $nxx) = $s =~ m{ \A 1? (\d{3}) (\d{3}) \d{4} \Z }xms; print defined($nxx) ? qq{npa '$npa', nxx '$nxx'} : 'invalid number' +; } " '15556667777' -> npa '555', nxx '666' '3334445555' -> npa '333', nxx '444' '999999999' -> invalid number '88888888' -> invalid number '121212121212' -> invalid number
Please see perlre, perlretut, and perlrequick.

Update: This approach to parsing is open to further regexish elaboration (I took a quick look at WP for NPA and NXX definitions, but I'm sure it's more involved):

c:\@Work\Perl\monks>perl -wMstrict -le "my $rx_npa = qr{ [2-9] (?! 11) \d\d }xms; my $rx_nxx = qr{ [2-9] (?! 11) \d\d }xms; my $rx_subs = qr{ \d{4} }xms; ;; for my $s (qw( 15556667777 x15156667777 15516667777x x15556167777x 3334415555 x3134145555 3314415555x x3334445555x 999999999 x999999999 999999999x x999999999x 88888888 x88888888 88888888x x88888888 121212121212 x121212121212 121212121212x x121212121212x 5119999999 15119999999 9995119999 19995119999 )) { printf qq{'$s' -> }; my $got_pn = my ($npa, $nxx, $subs) = $s =~ m{ (?<! \d) 1? ($rx_npa) ($rx_nxx) ($rx_subs) (?! \d) }xms; print $got_pn ? qq{npa '$npa', nxx '$nxx', subscriber '$subs'} : 'n +o number'; } " '15556667777' -> npa '555', nxx '666', subscriber '7777' 'x15156667777' -> npa '515', nxx '666', subscriber '7777' '15516667777x' -> npa '551', nxx '666', subscriber '7777' 'x15556167777x' -> npa '555', nxx '616', subscriber '7777' '3334415555' -> npa '333', nxx '441', subscriber '5555' 'x3134145555' -> npa '313', nxx '414', subscriber '5555' '3314415555x' -> npa '331', nxx '441', subscriber '5555' 'x3334445555x' -> npa '333', nxx '444', subscriber '5555' '999999999' -> no number 'x999999999' -> no number '999999999x' -> no number 'x999999999x' -> no number '88888888' -> no number 'x88888888' -> no number '88888888x' -> no number 'x88888888' -> no number '121212121212' -> no number 'x121212121212' -> no number '121212121212x' -> no number 'x121212121212x' -> no number '5119999999' -> no number '15119999999' -> no number '9995119999' -> no number '19995119999' -> no number


Give a man a fish:  <%-{-{-{-<


In reply to Re: Negative Index with "substr" Not Working by AnomalousMonk
in thread Negative Index with "substr" Not Working by he204035

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.