Reputation: 21224
I have the following Regex pattern:
(A|B|C)stuffinbetween(D|E|F)
Is there a way to match only the following patterns:
AstuffinbetweenD
BstuffinbetweenE
CstuffinbetweenF
?
(I want to match it without having to specify the fully unrolled Regex 3 times for 3 patterns...)
Upvotes: 2
Views: 86
Reputation:
Or, you could use:
String g1 = "stuffinbetween";
String regex = "(?:A" + g1 + "D|B" + g1 + "E|C" + g1 + "F)";
:p
Upvotes: 0
Reputation: 89629
If your regex flavor supports it you can use conditionals like this:
(?:(A)|(B)|C)stuffinbetween(?(1)D|(?(2)E|F))
(?(1)true|false)
: if group 1 is defined then "true" else "false"
To make the pattern more efficient
with PCRE, you can use the S modifier that can improve alternations:
~(?:(A)|(B)|C)stuffinbetween(?(1)D|(?(2)E|F))~S
always with PCRE or Perl, you can use the (*SKIP)
control verb to skip already tested letters (only if "stuffinbetween" doesn't contain A, B, or C):
~(?=[ABC])(?:(A)|(B)|C)stuffinbetween(*SKIP)(?(1)D|(?(2)E|F))~
With other flavors, you can start the pattern with a lookahead to quickly discards useless positions in the string:
(?=[ABC])(?:(A)|(B)|C)stuffinbetween(?(1)D|(?(2)E|F))
Upvotes: 2
Reputation:
Yes, you can!
(?:A(stuffinbetween)D|B\g<1>E|C\g<1>F)
It is because \g<1> recurses the 1st subpattern
.
Hope it helps.
Upvotes: 1
Reputation: 627469
You cannot do that without restricting the pattern.
Pattern restriction is achieved with lookarounds.
So, either use lookaheads (see demo):
(A(?=stuffinbetweenD)|B(?=stuffinbetweenE)|C(?=stuffinbetweenF))stuffinbetween(D|E|F)
Or lookbehinds:
(A|B|C)stuffinbetween((?<=Astuffinbetween)D|(?<=Bstuffinbetween)E|(?<=Cstuffinbetween)F)
Note that for the second method to work with patterns of undefined length you need a regex flavor that supports them (.NET, Pypi regex Python module, JG Soft).
Upvotes: 1