'Exclude substring from capture group

I am using a system which takes a PCRE compatible regular expression.

The system stores capture group 1 into a database.

I need to capture two halves of a string with a delimiter, excluding the delimiter, as a single capture group.

Given the string: "I want to capture this bit but not this bit and definitely this bit"

I get that I could create a regex like:

([A-Za-z\s]*) but not this bit([A-Za-z\s]*)

This would give me two capture groups: Group 1: "I want to capture this bit" Group 2: " and definitely this bit"

However, I miss out on half my result, as group 1 is all that is stored.



Solution 1:[1]

So it turned out I had to do this programmatically, rather than relying on a single regex. Turns out Casimir was correct that it wasn't possible to do this with a single capture group, even following hwnd's suggestion, as below:

branch-reset does not result in a combined capture group

Also, yes, I had the wrong slash :-P

Solution 2:[2]

You may be thinking about the branch reset feature. But this is only an assumption.

(?|([a-zA-Z\s]+) but not this bit|([a-zA-Z\s]+))

As stated in the comments, you can can fix this using the correct syntax.

([A-Za-z\s]+) but not this bit([A-Za-z\s]+)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aussieklutz
Solution 2