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

Yes,
thing is that even entry level hackers will just add whatever text they want, AFTER the numbers ext. and more numbers.........
I want to dis-allow ANY TEXT and allow any number of digits and the 2 exceptions of etc. and or Ext.
Not just a choice of numbers OR text,
but digits, only digits and NO TEXT anywhere, before or after the pemissible digits and ext. or Ext. This also question the use of the ^ and the $
as the only permissible string would be the digits with etc. or Ext.
I'd rather not just have the regex remove the offending text characters -
an error msg plus having to edit the fiels makes the spammers have to spend more effort to get their stuffe' posted.
  • Comment on Re^2: Tweak for my Perl Regex that screens for digits only

Replies are listed 'Best First'.
Re^3: Tweak for my Perl Regex that screens for digits only
by g0n (Priest) on Jan 25, 2006 at 19:41 UTC
    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".

      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)
      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.
Re^3: Tweak for my Perl Regex that screens for digits only
by SamCG (Hermit) on Jan 25, 2006 at 19:47 UTC
    g0n's reply does do what you asked for (mmm, with the exception that it also allows the plus sign (+), but you may want that too).