Re: regex matching exactly 10 digits



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."

.



Relevant Pages

  • Re: Regexp, ***= and subexpressions
    ... specified that the user's input be treated as a literal string rather than ... -exact] in cases where regexp isn't required:P ... Seems to be that regexp can't match word boundaries at "non-word" ... But now Earth has rotated and I can get back to sleep. ...
    (comp.lang.tcl)
  • Re: Regexp: Case-insensitive matching | N factorial
    ... a regexp component that matches a string of letters, ... Will match the string 'cat' anywhere it appears regardless of case. ... Javascript regular expressions have an alternative operator '|' (kind ...
    (comp.lang.javascript)
  • Re: How to get the "longest possible" match with Pythons RE module?
    ... Under most "NFA" implementation, such a regexp is ... searching a very long string that happens not to contain any matches. ... under many implementations it's very easy to end ... double-quote character at each end, and backslash escapes, ...
    (comp.lang.python)
  • Re: String search vs regexp search
    ... of occurences using string & regexp. ... I wrote the code for the regexp search as well as the function ... How long is the substring? ...
    (comp.lang.python)
  • Regular Expression Help
    ... I was wondering if someone could tell me where I'm going wrong with my regular expression. ... I'm trying to write a regexp that identifies whether a string contains a correctly-formatted currency amount. ... print str + " is not a match" ...
    (comp.lang.python)