how to match or detect alphanumeric including - or spaces through out

Discussion in 'Perl Misc' started by Jack, Aug 8, 2006.

  1. Jack

    Jack Guest

    Hi

    I have strings : 'SA-QA TAR TEL-LL' and 'SA QA-TA REG TEL-LL'
    and I want to test that this is a valid alphanumeric ; I will have
    strings which vary the space and - position. Looking to find a matcher
    that allows for a 0 or 1 spaces and 0 or more dashes throughout without
    overly complicating the regex.. I tried this but it started getting too
    case specific and complex:

    $temp = 'SAQA TAR sd-f44TELLL';
    if ($temp =~ m/^\w+\s?\w*\-*\w*\s?\w*\-*\w+\s?\w+$/) { print "
    alphanum "; } #
    # can try this also ! m/^\w+\s*w+$/

    Does anyone have a solution that could handle all combinations of
    alphanumeric and -, space

    thank you

    Jack
     
    Jack, Aug 8, 2006
    #1
    1. Advertisements

  2. Jack

    Paul Lalli Guest

    You need to better define what you mean. An "alphanumeric" is either
    an alphabetic or numeric character. Dashes and spaces are not
    alphanumerics. Therefore, your strings are not "valid alphanumeircs".
    So what do you actually mean?
    I don't at all understand what you're *trying* to do, but the answer to
    this question is:
    m/^[\w\s-]+$/

    If that doesn't apply to your situation, you need to post a
    short-but-complete script, along with sample input, desired output, and
    actual output, that demonstrates the problems you're having. Be sure
    to include at least a couple strings that should match and a couple
    that should not.

    Paul Lalli
     
    Paul Lalli, Aug 8, 2006
    #2
    1. Advertisements

  3. Jack

    Xicheng Jia Guest

    I supposed you want to find strings which are concatenation of words by
    non-repeating whitespace or hyphen, and meanwhile the string can not
    contain leading/trailing whitespaces or hyphenes. So:

    "This is-a valid-string"
    "This is- not a valid string" # space and hyphen connected
    "not--valid" # repeating of hyphenes
    "not valid" # repeating of spaces
    "-not valid" # leading hyphen
    "not valid " # trailing space

    If the above is what you wanted, then you are asking a FAQ regex
    question, and your pattern can be as following:

    ^\w+(?:[\s-]\w+)*$

    If you allow repeating hyphenes. but no connection between any hyphen
    and space, then change [\s-] to (?:\s|-+).........

    If still not, please make your question clearer, and provide us with
    valid/invalid sample data.

    Xicheng
     
    Xicheng Jia, Aug 8, 2006
    #3
  4. Jack

    Ben Morrow Guest

    That also matches _, which is not commonly considered alphanumeric.

    /^[[:alnum:]\s-]+$/

    or

    /^[[:alnum:] -]+$/

    depending on whether your definition of 'space' includes tab/CR/LF or
    not.
    Seconded :)

    Ben
     
    Ben Morrow, Aug 8, 2006
    #4
  5. /\A[[:alnum:]][[:alnum:] -]*[[:alnum:]]\z/



    John
     
    John W. Krahn, Aug 8, 2006
    #5
  6. Jack

    Xicheng Jia Guest

    Hi, John:

    this won't match a stand-alone alnum, say $str = "A";
    guess he may want to use lookaround for this purpose. :)

    Xicheng
     
    Xicheng Jia, Aug 8, 2006
    #6
  7. Jack

    Ben Morrow Guest

    No need for that:

    /\A [[:alnum:]] (?: [[:alnum:] -]* [[:alnum:]] )? \z/x

    Ben
     
    Ben Morrow, Aug 8, 2006
    #7
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.