0

If I want to capture a pipe character | in the capture group of a regular expression then it needs to be backslash escaped so it isn't interpreted as an alternator.

(and\|or) matches the string "and|or"
(and|or) matches the strings "and" and "or"

What other characters need to be escaped inside a capture group in order to be interpreted literally? Is there a comprehensive list of these characters?

Cory Klein
  • 51,188
  • 43
  • 183
  • 243
  • 3
    a capture group doesn't change what you must escape. In general all characters that have a special meaning in a regular expression must be escaped to be used as literal. However there are some exceptions depending of the flavour and the context. You can see an example for square brackets: http://stackoverflow.com/questions/17845014/what-does-the-regex-mean/17845034#17845034 – Casimir et Hippolyte Aug 26 '13 at 20:27
  • take a look: http://stackoverflow.com/questions/5105143/list-of-all-characters-that-should-be-escaped-before-put-in-to-regex – alecxe Aug 26 '13 at 20:28
  • What are you looking to achieve? – kevlar1818 Aug 26 '13 at 20:30
  • @kevlar1818 I'm not sure if it's really necessary to know for the question, but since you ask, I'm dynamically creating a regex and I need to escape characters that are supplied as input if they need to be interpreted literally. – Cory Klein Aug 26 '13 at 20:34
  • @CoryKlein you could just put each character into its own character set, which prevents the need to escape – vroomfondel Aug 26 '13 at 21:06
  • 1
    What programming language and/or regex library are you using? There will probably be a function to escape whatever needs escaping in this particular regex dialect, for example the Java [Pattern.quote](http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#quote(java.lang.String)) or perl [quotemeta](http://perldoc.perl.org/functions/quotemeta.html). – Ian Roberts Aug 26 '13 at 22:53
  • @IanRoberts I'm using QRegExp. From what I can tell, it doesn't provide such functionality. – Cory Klein Aug 27 '13 at 18:01

0 Answers0