Re: regex matching exactly 10 digits



Thanks I am reading the backtracking documentation now.
Paul Lalli wrote:
jtbutler78@xxxxxxxxxxx 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

.



Relevant Pages