in reply to Regex Start Anchor with variables

Hey guys,
Thx for that. Most examples use hardcoded regexes (for ease of reading I guess), so I couldn't figure it out.
Looks like \Q was what I needed.
Chris

Replies are listed 'Best First'.
Re^2: Regex Start Anchor with variables
by Hofmator (Curate) on Oct 05, 2006 at 12:20 UTC
    \Q or quotemeta is indeed what you needed.

    But your problem is not connected with hardcoded regexes vs. regexes in variables. As Samy_rio correctly pointed out, the problem comes from the fact that '.' has a special meaning in a regex which you don't want in your case. Therefore you need to escape it: either by putting a '\' in front of it - which works fine for the hardcoded case an is a bit more complicated for variables OR you use quotemeta resp. \Q which works in both cases.

    -- Hofmator

    Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.