Suppose I have the following text:
Yes: [x]
Yes: [ x]
Yes: [x ]
Yes: [ x ]
No: [x
No: x]
I am interested in a regex
expression with two capturing groups as follows:
group
$1
: match the brackets[
and]
that contain anx
in between with a variable amount of horizontal space aroundx
. I can use The fourth bird's answer based on a branch reset group for this:group
$2
: should match thex
that is contained in the brackets[
and]
. For this, I can use a non-capturing group with a combination of positive lookbehind and lookahead assertions:
The problem is that when I try to join the expressions by OR
, the second expression does not match anything. For example:
(?|(\[)(?=\h*x\h*])|(?<=\[)\h*x\h*(]))|(?:(?<=\[)\h*(x)(?=\h*]))
Results in (i.e., see demo with extended
flag enabled for clarity):
My intuition (i.e., perhaps not correct) is that there is no x
left to match for the second expression because the x
is matched in the first expression (i.e., group $0
). For example, simplifying the second expression to (?:(x))
(i.e., see demo) will match just fine the x
not comprised in brackets as shown below.
Therefore, I thought I should somehow reset the group $0
matches from the first expression. So I tried adding the \K
meta escape to the first expression before (])
, but that did not solve anything.
Also, as much as possible, I want to stick to the format (?|regex)|(?:regex)|...
because I want to be able to extend the expression further with other groups. I am using Oniguruma regular expressions and the PCRE
flavor. Do you have any ideas on how this can be accomplished?
P.S. Apologies if the title of the question is not entirely accurate.