Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Unrecognized escape \Q passed through in regex

by Articuno (Beadle)
on Jan 26, 2006 at 18:39 UTC ( #525809=note: print w/ replies, xml ) Need Help??


in reply to Re: Unrecognized escape \Q passed through in regex
in thread Unrecognized escape \Q passed through in regex

Ok,I'll go with something along the lines of the second solution...

I know the meaning of the switches... They are there because some regexen on DB are allowed to be "real regexes" and not just "bare words".

Maybe I'll emulate \Q...\E then...

$regexp =~ s/\\Q(.*?)\\E/quotemeta($1)/eg;
Should that do what I want ? :-)
update: /e ==> /eg
-- 6x9=42


Comment on Re^2: Unrecognized escape \Q passed through in regex
Download Code
Re^3: Unrecognized escape \Q passed through in regex
by ikegami (Pope) on Jan 26, 2006 at 18:52 UTC

    Not necessarily. It won't work for

    print($regexp) Expect Gives -------------- ------------ ---------- \Q*\E\Q*\E \*\* \*\Q*\E \Q**LOL** \*\*LOL\*\* \Q**LOL** \\Quit\\Exit \\Quit\\Exit \Quit\Exit \Qfoo\\E foo\\\\E foo\\

    Solution:

    my $in_quote = 0; $regexp =~ s/([^\\]|\\.)/ if ($in_quote) { if ($1 eq '\\E') { $in_quote = 0; '' } else { quotemeta($1) } } else { if ($1 eq '\\Q') { $in_quote = 1; '' } else { $1 } } /eg;

    Update: Alternative:

    $regexp =~ s/ \G ( (?:[^\\]|\\[^Q])* ) (?: \\Q (?:[^\\]|\\[^E])* (?:\\E)? )? / $1 . (defined($2) ? quotemeta($2) : '') /xge;

    Neither snippet is fully tested. In fact, both are known to be unable to handle regexps in which (?{...}) or (?{{...}}) are used. What's wrong with what I suggested in my earlier post?

      But what's wrong with what I suggested in my earlier post?

      The regexes are in a DB. In the beginning, all of them where full regexes (no need for \Q...\E, etc...). But now, some of them are created by an user with a GUI. These "new" regexes are "plain text", and I want to ignore the metacharacters of *them* only, and not of all the regexes. If I "quotemeta" any regex i see, I can end up quoting real regexes :-(

      Oh, and the interface is not in Perl, so I don't have quotemeta there. All I can do is surround with \Q...\E and hope it works, but as I found out (the bad way), it doesn't :-(
      -- 6x9=42
        quotemeta should be easy to implement in any language. Adding a slash in front of any characters other than [a-zA-Z0-9] should do the trick.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://525809]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (13)
As of 2014-04-18 17:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (471 votes), past polls