Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   regex matching exactly 10 digits (http://www.velocityreviews.com/forums/t900817-regex-matching-exactly-10-digits.html)

jtbutler78@comcast.net 11-28-2006 01:58 PM

regex matching exactly 10 digits
 
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/;


Paul Lalli 11-28-2006 02:07 PM

Re: regex matching exactly 10 digits
 
jtbutler78@comcast.net 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


jtbutler78@comcast.net 11-28-2006 02:17 PM

Re: regex matching exactly 10 digits
 
Thanks I am reading the backtracking documentation now.
Paul Lalli wrote:
> jtbutler78@comcast.net 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



Mr P 11-28-2006 03:16 PM

Re: regex matching exactly 10 digits
 

jtbutler78@comcast.net 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


John W. Krahn 11-28-2006 03:36 PM

Re: regex matching exactly 10 digits
 
jtbutler78@comcast.net 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

Paul Lalli 11-28-2006 04:14 PM

Re: regex matching exactly 10 digits
 
John W. Krahn wrote:
> jtbutler78@comcast.net 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


Dr.Ruud 11-28-2006 04:28 PM

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


Tad McClellan 11-29-2006 01:30 AM

Re: regex matching exactly 10 digits
 
Mr P <MisterPerl@gmail.com> wrote:

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



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


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas


All times are GMT. The time now is 10:21 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.