I have a regex that correctly captures a slash followed by a number in a string. The capturing group portion of the regex looks like this:
\(\d)+\\??
(some digits after a slash up to, but not including, a question mark) and there is more to the regex before and after this capturing group. Now I want to also include in my capturing group a optional specific prefix (call it “abc_”):
- The entire prefix (all four characters) must be there to be included in the captured group
- If no prefix is present then the digit portion of the capturing group is still captured
- if the prefix is partially there or some other prefix is there then the string does not match the regex.
Some examples:
abc_12345 is captured
12345 is captured
ab_12345 fails to match the regex
abc_ fails to match the regex
abcd_ fails to match the regex
How do I construct this?
If I understand you correctly, you want this:
The ?: operator transforms the group into a non-catching group. I don’t understand the part with the partial prefix. If you allow any content in front of the regex, you cannot deny a certain optional prefix. You need to have a clear separator in front of the pattern, like a white space in order to deny a prefix.