Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Tweak for my Perl Regex that screens for digits only

by g0n (Priest)
on Jan 25, 2006 at 19:41 UTC ( [id://525544]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Tweak for my Perl Regex that screens for digits only
in thread Tweak for my Perl Regex that screens for digits only

That expression permits only:
Numbers
Ext or ext
The specified punctuation
anywhere in the string. It fails to match if anything else is included.

Update: My test case is as follows:

use strict; use warnings; my @strings; $strings[0] = "44 (1234) 123398 Ext 123"; $strings[1] = "+44 (1234) 123398 Ext 123"; $strings[2] = "44 (1234) 123398 Ext 123 xxxxxxxxxxxxxxx"; $strings[3] ="416-967-1111 ext. 123 xxxxxxxxxxxxxxxxx"; my $counter=0; for my $string (@strings) { if ($string =~/^(?:Ext|\d|\)|\(|\.|\s|\+|\-)+$/i) { print "$counter good\n"; } $counter++; }

Which returns:

0 good

It fails to match with a leading +, which I haven't got to the bottom of yet, but rejects all the wrong strings. (I'd missed out a vital |, which stopped the + matching - thanks to ysth for spotting it).

--------------------------------------------------------------

"If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."

John Brunner, "The Shockwave Rider".

Replies are listed 'Best First'.
Re^4: Tweak for my Perl Regex that screens for digits only
by ysth (Canon) on Jan 26, 2006 at 11:33 UTC
    if ($string =~/^(?:Ext|\d|\)|\(|\.|\s|\+\-)+$/i)
    It fails to match with a leading +, which I haven't got to the bottom of yet, but rejects all the wrong strings.
    It only accepts the pair "+-", not individual +'s or -'s. Easier with a character class:
    if ($string =~/^(?:Ext|[-.+()\d\s])+$/i)
Re^4: Tweak for my Perl Regex that screens for digits only
by hackermike (Novice) on Jan 25, 2006 at 19:50 UTC
    OK,
    I put your code in my script and when I enter:
    416-967-1111 ext. 123 xxxxxxxxxxxxxx
    in the folrm field and submit the form
    The regex does not detect that there is all those x's after the numbers ext. and number sequence.
    If there is ANY text, anywhere in the field results, other than ext. or Ext. then I want the regex to generate an error message.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://525544]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-19 00:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found