in reply to creating a regular expression's alternation dynamically?
You don't want to apply quotemeta ("\Q...) to the whole regex, since that will make the | characters match literally. You also need to beware of having things earlier in the list that match a prefix of something later in the list, if what part of the string your regex matches matters to you. For instance:
print different things. To ensure that a list element is never matched where it is a prefix of another element that would match, sort the list by descending length.print "foobarbaz" =~ m/(foo|foobar|baz)/g; print "foobarbaz" =~ m/(foobar|foo|baz)/g;
See Re: Common Perl Idioms for an implementation.
|
|---|