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.
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.
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.
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.
*$ 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.
In your first example (a)?b\1c, \1 refers to your (a) group, it means you must have an a :

abac will matchbac will matchbc won't matchIn your second example (a?)b\1c, \1 refers to (a?), where a is optional :

abac will matchbac won't matchbc will matchThe back reference doesn't care of your external ? (in the first example), it only takes care of what is inside parenthesis.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With