Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex confusion - ? inside and outside of parenthesis

Tags:

regex

This regex:

(a)?b\1c

does not match "bc" while this one:

(a?)b\1c

does match it. Why is this? I thought these statements are identical.

like image 999
daremkd Avatar asked Nov 03 '13 14:11

daremkd


People also ask

Do you need to escape parentheses in regex?

Since parentheses are also used for capturing and non-capturing groups, we have to escape the opening parenthesis with a backslash. An explanation of how literalRegex works: / — Opens or begins regex.

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.

Can you use parentheses in regex?

Use Parentheses for Grouping and Capturing. By placing part of a regular expression inside round brackets or parentheses, you can group that part of the regular expression together. This allows you to apply a quantifier to the entire group or to restrict alternation to part of the regex.

What does '$' mean in regex?

*$ means - match, from beginning to end, any character that appears zero or more times. Basically, that means - match everything from start to end of the string.


1 Answers

In your first example (a)?b\1c, \1 refers to your (a) group, it means you must have an a :

enter image description here

  • abac will match
  • bac will match
  • bc won't match

In your second example (a?)b\1c, \1 refers to (a?), where a is optional :

enter image description here

  • abac will match
  • bac won't match
  • bc will match

The back reference doesn't care of your external ? (in the first example), it only takes care of what is inside parenthesis.

like image 89
zessx Avatar answered Sep 21 '22 01:09

zessx