Reputation: 774
In the following RegEx:
((this)|(that))-((?(2)these|(?(3)those)))
I accept one of two cases:
for this-these I get the Array of :
Array (
[0] => this-these
[1] => this
[2] => this
[3] =>
[4] => these
)
And for that-those I get the Array of :
Array (
[0] => that-those
[1] => that
[2] =>
[3] => that
[4] => those
)
The Array resembles the Caputred Groups, What I want is to Capture only the Group of these
and those
, I don't want to pull any other Groups to get the Array of :
case of this-these :
Array (
[0] => this-these
[1] => these
)
case of that-those :
Array (
[0] => that-those
[1] => those
)
What I've tried was the following RegExs:
(?:(this)|(that))-(?:(?(1)(these)|(?(2)(those))))
But got the Array of :
Array (
[0] => that-those
[1] =>
[2] => that
[3] =>
[4] => those
)
Then tried the RegEx:
(?:(?:this)|(?:that))-((?(1)(?:these)|(?(2)(?:those))))
Which was False because the Groups of (1), (2)
doesn't exist.
How can I Capture a Non-Capture Group to Apply a Condition on it or Capture only the Groups I want only.
Additonal Case Would Be:
this-in-these
this-on-those
that-at-those
that-as-these
Upvotes: 1
Views: 549
Reputation: 626690
After all, it appears you do not even need any conditional constructs, nor lookbehinds.
You are looking for grouping constructs, a combination of a capturing and non-capturing group:
(?:this-[io]n|that-a[ts])-(these|those)
See the regex demo
The (?:this-[io]n|that-a[ts])-(these|those)
will
(?:this-[io]n|that-a[ts])
- match either this-in
, this-on
, that-at
or that as
(due to the non-capturing group (?:...)
)-
- a hyphen(these|those)
- Capturing group 1: either these
or those
.Your original problem could be solved with a lookbehind:
(?:this|that)-((?<=this-)these|those)
See this regex demo. However, in case the this
or that
patterns can vary, the regex might not work, as most regex engines do not supprt lookbehind patterns of unknown width unless you are using .NET or JavaScript in the latest Chrome versions, or PyPi regex in Python.
Here, (?:this|that)
non-capturing group matches either this
or that
, then a hyphen is matched and then these
is captured into Group 1 if the current location is preceded with this-
, or those
is captured otherwise.
Upvotes: 1