aussieklutz
aussieklutz

Reputation: 147

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.

Upvotes: 0

Views: 582

Answers (2)

aussieklutz
aussieklutz

Reputation: 147

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

Upvotes: 0

hwnd
hwnd

Reputation: 70722

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]+)

Upvotes: 2

Related Questions