Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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)

Comment on regex testing for multiple values
Download Code
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 <c> block looks like an artifact from his HTML, if that's what you mean--it shouldn't show up in your code...

        That is, he was saying if you aren't going to use forward slashes to delimit your regex, then you need to prefix it with m:

         if ($blah =~ m#regex here#options here) instead of the usual  $blah =~ /regex here/options here

        or what have you. (In your post, the regex doesn't appear to start with a forward slash.)

        I'm a peripheral visionary... I can see into the future, but just way off to the side.
        ...all I get is a syntax error mismatched right bracket

        That's not what I get when I run your snippet (commenting out the do something lines). I get:

        syntax error at [...] line 12, near "{"

        where lines 12-13 are:

        elsif {

        Did you perchance mean else rather than elsif?

        if ($Session->{'usrSystem'}=~/{IND|NOR|PRO|ARM|SND/s)
        I don't understand why its not executing the code block, all I get is a syntax error mismatched right bracket for now I wrote as a series of if statements its ugly but it works.
        See bolded areas.
      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
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2015-07-08 03:59 GMT
Find Nodes?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...

    Results (94 votes), past polls