in reply to /x modifier with \Q and \E

/x apparently has no effect. \Q is what affects the space (for \Q implies to quote any characters following it, till the end or upto \E if present).

# Need perl to be built with DEBUGGING perl -Dr -e '$p = qr{\Q ++ \E} ; $q = qr{\Q ++ \E}x ; $r = qr{\Q++\E}' Compiling REx `\ \+\+\ ' size 3 Got 28 bytes for offset annotations. first at 1 rarest char + at 1 1: EXACT < ++ >(3) 3: END(0) anchored " ++ " at 0 (checking anchored isall) minlen 4 Offsets: [3] 1[8] 0[0] 9[0] Compiling REx `\ \+\+\ ' size 3 Got 28 bytes for offset annotations. first at 1 rarest char + at 1 1: EXACT < ++ >(3) 3: END(0) anchored " ++ " at 0 (checking anchored isall) minlen 4 Offsets: [3] 1[8] 0[0] 9[0] Compiling REx `\+\+' size 3 Got 28 bytes for offset annotations. first at 1 rarest char + at 0 1: EXACT <++>(3) 3: END(0) anchored "++" at 0 (checking anchored isall) minlen 2 Offsets: [3] 1[4] 0[0] 5[0] Omitting $` $& $' support. EXECUTING... Freeing REx: `"\\ \\+\\+\\ "' Freeing REx: `"\\ \\+\\+\\ "' Freeing REx: `"\\+\\+"'

I consulted perlre to see if there was anything about a space with \Q & /x. Finding nothing there, I moved to the pointer provided to Gory details of parsing quoted constructs in perlop. That section also did not list anything about the topic. The reading of that section, however, was interesting. All was not in vain.

Replies are listed 'Best First'.
Re^2: /x modifier with \Q and \E
by wardy3 (Scribe) on Apr 01, 2008 at 05:40 UTC
    Thanks parv - I thought I'd try the regex debugger too just before I posted the message. I saw that it was trying to anchor ' ++ ', which prompted my question at the end of the original post. It surprised me and I thought I'd lost the plot somewhere along the line.

    I wanted to make it possible for my script to handle special regex characters (like the +) so at this point, I think I'll just write cluttered regex's unless someone can point out how to do it :-)

    Thanks

      I suppose you are aware of quotemeta function, which you could use for the same effect of \Qpattern\E something like (one of many variations) ...

      my @patterns = map { quotemeta $_ } @string

      In any case, as the Gory details says, be aware of what happens with [$@] in \Q (or quotemeta) including other meta characters.

        Brilliant. I was aware of quotemeta but I had totally forgotten. I was even considering writing my own version :-(

        Thanks for the help - that's what I'll use.