D.R.
D.R.

Reputation: 21224

Regex: pattern based on previous capture group

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

Answers (4)

user4227915
user4227915

Reputation:

Or, you could use:

String g1 = "stuffinbetween";
String regex = "(?:A" + g1 + "D|B" + g1 + "E|C" + g1 + "F)";

:p

Upvotes: 0

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89629

If your regex flavor supports it you can use conditionals like this:

(?:(A)|(B)|C)stuffinbetween(?(1)D|(?(2)E|F))

demo

(?(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))~

demo

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

user4227915
user4227915

Reputation:

Yes, you can!

(?:A(stuffinbetween)D|B\g<1>E|C\g<1>F)

It is because \g<1> recurses the 1st subpattern.

Regex live here.

Hope it helps.

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

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

Related Questions