The following test results help show the basic semantics used by regular expression implementations that, as of 2012, support capturing groups within variable-length lookbehind.
Regex | Target | Result | ||||
---|---|---|---|---|---|---|
.NET | Java | PCRE | ActionScript | JGsoft | ||
(?<=(.+))$ |
"abc" |
$1 : "abc" |
Unsupported | Unsupported | Unsupported | $1 : "abc" |
(?<=(.+?))$ |
"abc" |
$1 : "c" |
Unsupported | Unsupported | Unsupported | $1 : "c" |
(?<=(.{1,3}))$ |
"abc" |
$1 : "abc" |
$1 : "c" |
Unsupported | Unsupported | $1 : "abc" |
(?<=(.{1,3}?))$ |
"abc" |
$1 : "c" |
$1 : "c" |
Unsupported | Unsupported | $1 : "c" |
(?<=(.{1,3})(.{1,3}?))$ |
"abc" |
|
|
Unsupported | Unsupported |
|
(?<=(.{1,3}?)(.{1,3}))$ |
"abc" |
|
|
Unsupported | Unsupported |
|
(?<=(.)|(..)|(...))$ |
"abc" |
|
|
|
|
|
(?<=(...)|(..)|(.))$ |
"abc" |
|
|
|
|
|
Other regex flavors:
(?<=a|bc)
and (?<=a(?:b|c)|a)
, but not (?<=ab?)
or (?<=a(?:b|cd))
. It also doesn't support capturing groups in lookbehind.(?<=abc)
or (?<=a|b)
..NET's lookbehind is the flagship and ideal. It supports all regex syntax, and provides intuitive greedy and nongreedy behavior. It does this while maintaining the same efficiency as lookahead. Java's lookbehind behavior is different, although this is only observable when capturing groups are used within lookbehind. Java applies lookbehind by going through the target string from right to left, while going through the regex from left to right. .NET goes through both the target string and the regex from right to left, except that the order of alternation is unchanged.
Consider the regex
(?<=(.{1,3}))$
. In Java, backreference 1 when this is tested against"abc"
is"c"
(unlike .NET, where it's"abc"
)..NET applies the regex in reverse, applying it against all text available to the left of the current matching position. It starts after
"c"
..{1,3}
matches three characters, because the quantifier is greedy. The regex engine then concludes the match attempt has succeeded.Java starts with going back one character, i.e., starting between
"b"
and"c"
, applying the regex from left to right..{1,3}
can match one character, so the lookbehind succeeds.Next, consider the regex
(?<=(...)|(..)|(.))$
. When tested against"abc"
in Java, backreference 3 is"c"
(unlike .NET, where backreference 1 is"abc"
).Here, the story is the same as before. .NET goes in reverse through the regex and through the target string, but still processes alternation from left to right. Going backwards starting after
"c"
,...
finds a match. Alternation is eager so the lookbehind succeeds immediately.Java goes back one character.
...
fails to match because"c"
is the only character available...
also fails. The lone dot then matches"c"
.If you had
(?<=(abc)|(de)|(f))
and the string"abc"
in Java, then it would start before the"c"
. Firstabc
fails to match"c"
, thende
fails to match"c"
, and thenf
fails to match"c"
. Java then looks back one more character.abc
fails to match"bc"
,de
fails to match"bc"
, andf
fails to match"bc"
. Going back one more character,abc
matches"abc"
, and the lookbehind succeeds.The reason Java doesn't support infinite repetition is that it would have to test the lookbehind regex at every character in the target string before the current matching position, which would be inefficient with long target strings. .NET only applies the lookbehind regex once, which is no more expensive than a lookahead.
— Adapted from email correspendence between Jan Goyvaerts and Steven Levithan, while working on Regular Expressions Cookbook, Second Edition
Miscellaneous notes: