Reputation: 6241
Is there a way to match a B
only if preceded by an A
? The A
can be at any position behind the B
, with any amount of characters between. Examples:
A_B (Matches `B`)
C_B (No match)
I've tried:
(?=A)[^B]*B
But it matches all the characters preceeding B
as well. My regex engine does not support variable length look-behinds. Is there any way I can do this?
Edit: I am currently using the built in regex search in Eclipse, however, I am using regex101.com to test things out.
Upvotes: 1
Views: 49
Reputation: 726899
A work-around for the lack of variable-length lookbehind is available in situations when your strings have a relatively small fixed upper limit on their length. For example, if you know that strings are at most 100 characters long, you could use {0,100}
in place of *
or {1,100}
in place of +
inside the lookbehind expression:
(?<=A[^B]{0,100})B
When the length of your string has no obvious upper limit, you could drop lookbehind altogether, use a non-capturing group in its place, place a capturing group over B
, and use the content of that group as the result of your regular expression.
Upvotes: 2