in reply to regex problem with metachars

Use \Q and \E surrounding your possibly offending characters.   This will 'quote' any strange and nasty characters by escaping them with '\'.   Read about these in perlre.

Replies are listed 'Best First'.
Re: Re: regex problem with metachars
by sweetblood (Prior) on Nov 07, 2003 at 18:33 UTC
    Thanks for your reply but, but by using \Q it also would put a splash in front of any escaped character such as "\t". The same goes for quotemeta().

      That's because \t is a regex metacharacter. If you only want to escape some metacharacters, you'll need to figure out which ones.

      If you want to escape all metacharacters beside backslash, something like:

      s/([^A-Za-z_0-9\\])/\\$1/g;
      should do it.

      If the delimiter will always be one character then you can assume anything longer than 1 character is a escape sequence like "\t". So you just call quotemeta() on delimiters with a length of 1.

      HTH

      Tedrek

        That's one I haven't thought of. I try that out. The delimiter should never be more than 1 character.

        Thanks!