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

You want to list all the alternatives, separated by vertical bars. For individual character alternatives, you can create a character class (a list or range of characters inside square brackets). For example:
/^(?:[-()\d\s]|[Ee]xt\.)*$/
matches a series of (any combination of) only hyphen, left-paren, right-paren, digits, whitespace, or Ext. or ext.

Updated: added the hyphen. Note that a hyphen, if it appears in a character class, must be the first listed character (so that it doesn't look like part of a character range).


Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^4: Tweak for my Perl Regex that screens for digits only
by hackermike (Novice) on Jan 25, 2006 at 20:09 UTC
    THANKS ROY! Apologies for being slow to acknowledge your reply. I goofed on first testing your code but then on inspection cleared that up and find that your regex does exactly as expected and is what I wanted, simply, and effectively,
    Although I would like to get the hyphen included with the permissible characters, and wonder whether it will require the escape backslash? Thank you for your very kind condsideration of this hapless hacker!
    Mike
    I WROTE:
    I have this regex in my simple perl script handling a html form:
    unless ($FORM{'phone'} =~ /\s*\(*\)*\.*\d+\-*\s*/) {
    ...
    the point of which is to disallow text in the field whilst allowing for various punctuation styles. Hoping to avoid re-inventing the whole system, I'd like to be able to allow certain text, i.e. ext. or Ext. Any ideas? thx, mike

    YOU WROTE:
    You want to list all the alternatives, separated by vertical bars. For individual character alternatives, you can create a character class (a list or range of characters inside square brackets).
    For example:
    /^(?:()\d\s|Eext\.)*$/
    matches a series of (any combination of) only left-paren, right-paren, digits, whitespace, or Ext. or ext.
Re^4: Tweak for my Perl Regex that screens for digits only
by hackermike (Novice) on Jan 25, 2006 at 20:50 UTC
     /^(?:[-()\d\s]|[Ee]xt\.)*$/
    matches a series of (any combination of) only hyphen, left-paren, right-paren, digits, whitespace, or Ext. or ext. Updated: added the hyphen. Note that a hyphen, if it appears in a character class, must be the first listed character (so that it doesn't look like part of a character range).

    THANKS!
    My seeking help with making the ext. permissible actually solved two problems, as ikegami pointed out, my poor effort was not even doing what I thought it would!

    Now if there only some way for perl to limit the number of characters in a text field, (not a text area and not the phone field)so they can't enter whole paragraphs in the title field............
    Mike
      You can truncate a string variable to whatever length you like by using substr.
      substr($text_field, 100) = '' if length($text_field) > 100;
      will truncate $text_field to 100 chars. Or you could have your program just abort if it received an oversized field.

      Caution: Contents may have been coded under pressure.
        Aborting with a not so subtle err msg would be the way to go.
        Point would be that legit users prob would follow directions, (well, fairly well) and the more time spammers have to waste reading and responding to windy err msg's the better!
        I've not used substr, how can it be written to generate an err msg if the length exceeds X chars?
        Thx, Mike