> Would this be correct:  /foo|bar/

Yes.

> Should they be grouped thusly: /(foo|bar)/

This works too, but has the side effect of setting $1 to the matched value,
either "foo" or "bar".

> What about /(?:foo|bar)/   ?

This is ok too, but the parens aren't necessary.  If you wanted to search
for "foobar" or "foooof" you would need the parens.

/foo(?:bar|oof)/

In this case you need the parens to group the alternation part of the
expression.

Rob


-----Original Message-----
From: Rich Fernandez [mailto:[EMAIL PROTECTED]
Sent: Thursday, May 20, 2004 4:23 PM
To: '[EMAIL PROTECTED]'
Subject: regex alternation question


I'm unclear about how alternation works in a regex.
 
Say I want to find either "foo" or "bar" within a string. I don't care which
one I match, only that I make a match.
Would this be correct:  /foo|bar/
 
Should they be grouped thusly: /(foo|bar)/
 
What about /(?:foo|bar)/   ?
 
Specifically, I know that the second one remembers which one is found and
the third one doesn't. But is the first example wrong? 
 
TIA
 
richf

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to