regex matching exactly 10 digits

Discussion in 'Perl Misc' started by jtbutler78@comcast.net, Nov 28, 2006.

  1. Guest

    I am having an issue trying to match exactly 10 digits. I want to
    append a '1' in front of 10 digit fax numbers. Numbers longer than 10
    digits leave alone. I am using \d{10,10} match at least and at most 10
    digits but it still appends a 1 to international numbers. I am missing
    something but I am not sure what.

    #strip out unneeded chars
    $fax_num =~ s/[\s()-]//g;

    #append 1 to 10 digit fax number
    $fax_num =~ s/(\d{10,10})/1$1/;
    , Nov 28, 2006
    #1
    1. Advertising

  2. Paul Lalli Guest

    wrote:
    > I am having an issue trying to match exactly 10 digits. I want to
    > append a '1' in front of 10 digit fax numbers. Numbers longer than 10
    > digits leave alone. I am using \d{10,10} match at least and at most 10


    Otherwise known as \d{10}

    > digits but it still appends a 1 to international numbers. I am missing
    > something but I am not sure what.
    >
    > #strip out unneeded chars
    > $fax_num =~ s/[\s()-]//g;
    >
    > #append 1 to 10 digit fax number
    > $fax_num =~ s/(\d{10,10})/1$1/;


    You are missing the fact that a regexp doesn't care what *else* is in
    the string. You are checking only to see if the string *contains* the
    pattern. "At most" does not mean that the string cannot contain more
    of that token. It means that this particular piece of the regexp will
    not match more than that many. You need to explicitly check that
    another digit neither precedes nor follows the ten that you've found:

    s/(?<!\d)(\d{10})(?!\d)/1$1/;

    Read more about look-ahead and look-behind assertions in:
    perldoc perlre
    perldoc perlreref

    Paul Lalli
    Paul Lalli, Nov 28, 2006
    #2
    1. Advertising

  3. Guest

    Thanks I am reading the backtracking documentation now.
    Paul Lalli wrote:
    > wrote:
    > > I am having an issue trying to match exactly 10 digits. I want to
    > > append a '1' in front of 10 digit fax numbers. Numbers longer than 10
    > > digits leave alone. I am using \d{10,10} match at least and at most 10

    >
    > Otherwise known as \d{10}
    >
    > > digits but it still appends a 1 to international numbers. I am missing
    > > something but I am not sure what.
    > >
    > > #strip out unneeded chars
    > > $fax_num =~ s/[\s()-]//g;
    > >
    > > #append 1 to 10 digit fax number
    > > $fax_num =~ s/(\d{10,10})/1$1/;

    >
    > You are missing the fact that a regexp doesn't care what *else* is in
    > the string. You are checking only to see if the string *contains* the
    > pattern. "At most" does not mean that the string cannot contain more
    > of that token. It means that this particular piece of the regexp will
    > not match more than that many. You need to explicitly check that
    > another digit neither precedes nor follows the ten that you've found:
    >
    > s/(?<!\d)(\d{10})(?!\d)/1$1/;
    >
    > Read more about look-ahead and look-behind assertions in:
    > perldoc perlre
    > perldoc perlreref
    >
    > Paul Lalli
    , Nov 28, 2006
    #3
  4. Mr P Guest

    wrote:
    > I am having an issue trying to match exactly 10 digits. I want to
    > append a '1' in front of 10 digit fax numbers. Numbers longer than 10
    > digits leave alone. I am using \d{10,10} match at least and at most 10
    > digits but it still appends a 1 to international numbers. I am missing
    > something but I am not sure what.
    >
    > #strip out unneeded chars
    > $fax_num =~ s/[\s()-]//g;
    >
    > #append 1 to 10 digit fax number
    > $fax_num =~ s/(\d{10,10})/1$1/;


    perhaps

    s|^\d{10}$|1$1|;

    although not knowing your delimiters, or if there can be more than one
    10-digit # per scalar, it's difficult to say with certainty.

    Have a nice holiday
    Mr P, Nov 28, 2006
    #4
  5. wrote:
    > I am having an issue trying to match exactly 10 digits. I want to
    > append a '1' in front of 10 digit fax numbers. Numbers longer than 10
    > digits leave alone. I am using \d{10,10} match at least and at most 10
    > digits but it still appends a 1 to international numbers. I am missing
    > something but I am not sure what.
    >
    > #strip out unneeded chars
    > $fax_num =~ s/[\s()-]//g;
    >
    > #append 1 to 10 digit fax number
    > $fax_num =~ s/(\d{10,10})/1$1/;


    It looks like that instead of appending a '1' you really want to prepend a
    '1'. You could do it like this:

    $fax_num = "1$fax_num" if $fax_num =~ tr/0-9// == 10;



    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Nov 28, 2006
    #5
  6. Paul Lalli Guest

    John W. Krahn wrote:
    > wrote:
    > > I am having an issue trying to match exactly 10 digits. I want to
    > > append a '1' in front of 10 digit fax numbers. Numbers longer than 10
    > > digits leave alone. I am using \d{10,10} match at least and at most 10
    > > digits but it still appends a 1 to international numbers. I am missing
    > > something but I am not sure what.
    > >
    > > #strip out unneeded chars
    > > $fax_num =~ s/[\s()-]//g;
    > >
    > > #append 1 to 10 digit fax number
    > > $fax_num =~ s/(\d{10,10})/1$1/;

    >
    > It looks like that instead of appending a '1' you really want to prepend a
    > '1'. You could do it like this:
    >
    > $fax_num = "1$fax_num" if $fax_num =~ tr/0-9// == 10;
    >


    If we're presuming that the only thing in $fax_num is the fax number
    itself, then there's no reason for any checking other than the lenth:

    $faxnum = "1$fax_num" if length($faxnum) == 10;

    Paul Lalli
    Paul Lalli, Nov 28, 2006
    #6
  7. Dr.Ruud Guest

    bugbear schreef:
    > Paul Lalli wrote:
    >> [attribution removed by bugbear]:


    >>> #append 1 to 10 digit fax number
    >>> $fax_num =~ s/(\d{10,10})/1$1/;

    >>
    >> You are missing the fact that a regexp doesn't care what *else* is in
    >> the string. You are checking only to see if the string *contains*
    >> the pattern. "At most" does not mean that the string cannot contain
    >> more of that token. It means that this particular piece of the
    >> regexp will not match more than that many. You need to explicitly
    >> check that another digit neither precedes nor follows the ten that
    >> you've found:

    >
    > Depending on the OP's data, the simpler match
    > /^\d{10,10}$/
    > may serve.


    echo 9876543210 | perl -wpe '
    s/^(?=[0-9]{10}$)/x/
    '
    x9876543210

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Nov 28, 2006
    #7
  8. Mr P <> wrote:

    > perhaps
    >
    > s|^\d{10}$|1$1|;



    What do you expect to be in $1 there on the "RHS"?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 29, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    1
    Views:
    938
    Ilias Lazaridis
    Sep 22, 2006
  2. blaine
    Replies:
    6
    Views:
    438
    blaine
    Apr 28, 2008
  3. harijay
    Replies:
    6
    Views:
    647
    harijay
    Nov 21, 2008
  4. Jack
    Replies:
    4
    Views:
    145
    Ben Morrow
    Jul 14, 2006
  5. ReMo...
    Replies:
    15
    Views:
    157
Loading...

Share This Page