in reply to Re: Re: Re: Re: Another regexp question
in thread Another regexp question
m/ ^bing | bong | bang\d\d /x;
I used an "extended regular expression" so that I could group each subexpression (each alternate) on its own line. If you want the ^ to bind to all three, and the \d\d to bind to all three, you must use parenthesis to constrain the alternation. And if you aren't trying to capture, use non-capturing parens:
m/^b(?:i|o|a)ng\d\d/;
(Note: I factored out everything that is common to all three alternates. That step is unnecessary. You could use (?:bing|bong|bang) too.)
Alternation may be the best route to follow. But sometimes when you see it factored down as the previous example, you might suddenly realize, hey, I can do this with a character class too:
m/^b[ioa]ng\d\d/;
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Re: Re: Another regexp question
by wolis (Scribe) on Nov 24, 2003 at 02:54 UTC |