Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

regex testing for multiple values

by grashoper (Monk)
on Mar 10, 2008 at 15:52 UTC ( #673259=perlquestion: print w/replies, xml ) Need Help??
grashoper has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to test for different account codes using a regex and can't quite get it to work as desired, keep getting a syntax error, I am not sure what is wrong so was hoping one of the esteemed monks here would give me a clue. I really need for this check to encompass multiple values and am only looking to match these 3 letter account codes and do something if there is a match.
if ($Session->{'usrSystem'} =~"?:SND|ARM|PRO|NOR|IND\"*/s)

Replies are listed 'Best First'.
Re: regex testing for multiple values
by ack (Deacon) on Mar 10, 2008 at 19:39 UTC

    I think what all these responses are trying to say is that you want something like:

    if ($session->{'usrSystem'} =~ m/(?:(?:SND|ARM|PRO|NOR|IND|) )*/s

    The (?: ) just tell the parser to not capture the matched strings (i.e., not to produce the $1.. variables).

    I presumed from your version of the Regex that you wanted to match 0 or more occurances of the 3 letter account codes (which was why you put the * in it).

    I also presume you meant to have a white-space character (or more specifically a blank) since you put a space between the \ and the " that right?

    I also presume that you put the /s at the end to treat the string a single line and that . will match embedded newline characters. Is that correct?

    ack Albuquerque, NM
      I don't really want the space but your other assumptions are correct,I will try your suggested statement, and post back if I have any further problems. Thank you for the help.
Re: regex testing for multiple values
by moritz (Cardinal) on Mar 10, 2008 at 15:55 UTC
    Regexes should be delimited by / ... / or start with an <m>, like this: m{SND|ARM|PRO|NOR|IND} or m/.../.

    single quotes delimit strings, not regexes.

    Update: fixed markup, Fletch++

      the usrsystem is a session variable as i am using iis and asp and activestate, I still get the syntax error not sure what I am doing wrong, what is the <c> block in your example do?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://673259]
Approved by Corion
[choroba]: erix Yes, but in the later concert :-)

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2017-12-12 14:59 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (335 votes). Check out past polls.