Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: Can a regular expression include an arbitrary string variable?by davido (Cardinal) |
on Oct 17, 2019 at 05:01 UTC ( [id://11107579]=note: print w/replies, xml ) | Need Help?? |
Yes, regular expressions can interpolate variables into them:
You may also decide you don't need a pattern match, and if you can come to that conclusion you avoid the problem of treating strings as code:
You can observe the rules of how interpolation works in regular expressions by reading perlop and perlre. The index function is also documented at perldoc -f index. An advantage of the index approach is that you're not exposing the regular expression mini-language and engine to arbitrary strings, which could contain code. It's also lighter-weight, not invoking the engine. Dave
In Section
Seekers of Perl Wisdom
|
|