0

The RegEx:

^([0-9\.]+)\Q|\E([^\Q|\E])\Q|\E

does not match the string:

1203730263.912|12.66.18.0|

Why?

4
  • you have to espace the pipe. What regex engine are you using? Commented Jul 4, 2014 at 15:15
  • another community? Sure! go ahead. I'm pretty sure you'll return back... Commented Jul 4, 2014 at 15:18
  • \Q \E should be escaping enough for any scenario in my book. Regarding the recent hate-attacks on my questions. Either they are racist or something else, but I am receiving a torrent of downvotes to eligble questions, since I change my display name. Commented Jul 7, 2014 at 22:16
  • No. I think its an unique question. Commented Jul 7, 2014 at 22:26

2 Answers 2

2

From PHP docs,

\Q and \E can be used to ignore regexp metacharacters in the pattern.

For example:

\w+\Q.$.\E$ will match one or more word characters, followed by literals .$. and anchored at the end of the string.

And your regex should be,

^([0-9\.]+)\Q|\E([^\Q|\E]*)\Q|\E 

OR

^([0-9\.]+)\Q|\E([^\Q|\E]+)\Q|\E 

You forget to add + after [^\Q|\E]. Without +, it matches single character.

DEMO

Explanation:

  • ^ Starting point.
  • ([0-9\.]+) Captures digits or dot one or more times.
  • \Q|\E In PCRE, \Q and \E are referred to as Begin sequence. Which treats any character literally when it's included in that block. So | symbol in that block tells the regex engine to match a literal |.
  • ([^\Q|\E]+) Captures any character not of | one or more times.
  • \Q|\E Matches a literal pipe symbol.
Sign up to request clarification or add additional context in comments.

3 Comments

Giving you the answer for great information, althouh it does not work in Python on your Demo page, strangly this does: ^([0-9\.]+)\|([0-9\.]+)\|
PCRE supports this feature I donno whether python supports this or not.
Good explanation for \Q\E, but in this case it's compatible with ^([0-9.]+)\|([^|]+)\| and simpler. Demo: regex101.com/r/sB6pV0/6 Note: characters inside character classes don't need escaping (with a few minor exceptions), you can just escape | in this case like: \|
1

The accepted answer seems somewhat incorrect so I wanted to address this for future readers.

If you did not already know, using \Q and \E ensures that any character between \Q ... \E will be matched literally, not interpreted as a metacharacter by the regular expression engine.

First and most important, \Q and \E is NOT usable within a bracketed character class [].

[^\Q|\E] # Incorrect [^|] # Correct 

Secondly, you do not follow that class with a quantifier. Using this, the correct syntax would be:

^([0-9.]+)\Q|\E([^|]+)\Q|\E 

Although, it is much simpler to write this out as:

^([0-9.]+)\|([^|]+)\| 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.