regex matching exactly 10 digits

J

jtbutler78

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/;
 
P

Paul Lalli

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
 
J

jtbutler78

Thanks I am reading the backtracking documentation now.
Paul said:
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
 
M

Mr P

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
 
J

John W. Krahn

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
 
P

Paul Lalli

John said:
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
 
D

Dr.Ruud

bugbear schreef:
Paul said:
[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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top