Find any word that occurs between a pair of HTML bold
tags, without including the tags in the regex match. For instance, if
the subject is My
<b>cat</b> is furry
, the only valid match should
be cat
.
(?<=<b>)w+(?=</b>)
Regex options: Case insensitive |
Regex flavors: .NET, Java, PCRE, Perl, Python, Ruby 1.9 |
JavaScript and Ruby 1.8 support the lookahead ‹(?=</b>)
›, but not the lookbehind ‹(?<=<b>)
›.
The four kinds of lookaround groups supported by modern regex flavors have the special property of giving up the text matched by the part of the regex inside the lookaround. Essentially, lookaround checks whether certain text can be matched without actually matching it.
Lookaround that looks backward is called lookbehind. This is the only
regular expression construct that will traverse the text from right to
left instead of from left to right. The syntax for positive
lookbehind is ‹(?<=⋯)
›. The four characters ‹(?<=
›
form the opening bracket. What you can put inside the lookbehind, here
represented by ‹⋯
›, varies among regular
expression flavors. But simple literal text, such as ‹(?<=<b>)
›, always
works.
Lookbehind checks to see whether the text inside the lookbehind
occurs immediately to the left of the position that the regular
expression engine has reached. If you match ‹(?<=<b>)
› against My <b>cat</b> is
furry
, the lookbehind will fail to match until the regular
expression starts the match attempt at the letter c
in the subject. The
regex engine then enters the lookbehind group, telling it to look to
the left. ‹<b>
›
matches to the left of c
. The engine exits the lookbehind at
this point, and discards any text matched by the lookbehind from the
match attempt. In other words, the match-in-progress is back at where
it was when the engine entered the lookbehind. In this case, the
match-in-progress is the zero-length match before the c
in the subject
string. The lookbehind only tests or asserts that ‹<b>
› can be matched; it
does not actually match it. Lookaround constructs are therefore called
zero-length assertions.
After the lookbehind has matched, the shorthand character class
‹w+
› attempts to match
one or more word characters. It matches cat
. The ‹w+
› is not inside any kind of lookaround or
group, and so it matches the text cat
normally. We say that ‹w+
› matches and consumes cat
, whereas lookaround
can match something but can never consume anything.
Lookaround that looks forward, in the same direction that the
regular expression normally traverses the text, is called lookahead. Lookahead is equally
supported by all regex flavors in this book. The syntax for
positive lookahead is ‹(?=⋯)
›. The three characters ‹(?=
› form
the opening bracket of the group. Everything you can use in a regular
expression can be used inside lookahead, here represented by ‹⋯
›.
When the ‹w+
› in
‹(?<=<b>)w+(?=</b>)
› has matched
cat
in
My
<b>cat</b> is furry
, the regex engine enters
the lookahead. The only special behavior for the lookahead at this
point is that the regex engine remembers which part of the text it has
matched so far, associating it with the lookahead. ‹</b>
› is then matched
normally. Now the regex engine exits the lookahead. The regex inside
the lookahead matches, so the lookahead itself matches. The regex
engine discards the text matched by the lookahead, by restoring the
match-in-progress it remembered when entering the lookahead. Our
overall match-in-progress is back at cat
. Since this is also the end of our
regular expression, cat
becomes the final match
result.
‹(?!⋯)
›, with an exclamation
point instead of an equals sign, is negative
lookahead. Negative lookahead works just like positive
lookahead, except that whereas positive lookahead matches when the
regex inside the lookahead matches, negative lookahead matches when
the regex inside the lookahead fails to match.
The matching process is exactly the same. The engine saves the match-in-progress when entering the negative lookahead, and attempts to match the regex inside the lookahead normally. If the sub-regex matches, the lookahead fails, and the regex engine backtracks. If the sub-regex fails to match, the engine restores the match-in-process and proceeds with the remainder of the regex.
Similarly, (?<!⋯)
is
negative lookbehind. Negative lookbehind
matches when none of the alternatives inside the lookbehind can be
found looking backward from the position the regex has reached in the
subject text.
Lookahead is easy. All regex flavors discussed in this book allow you to put a complete regular expression inside the lookahead. Everything you can use in a regular expression can be used inside lookahead. You can even nest other lookahead and lookbehind groups inside lookahead. Your brain might get into a twist, but the regex engine will handle everything nicely.
Lookbehind is a different story. Regular expression software has always been designed to search the text from left to right only. Searching backward is often implemented as a bit of a hack: the regex engine determines how many characters you put inside the lookbehind, jumps back that many characters, and then compares the text in the lookbehind with the text in the subject from left to right.
For this reason, the earliest implementations allowed only fixed-length literal text inside lookbehind. Perl and Python still require lookbehind to have a fixed length, but they do allow fixed-length regex tokens such as character classes, and allow alternation as long as all alternatives match the same number of characters.
PCRE and Ruby 1.9 take this one step further. They allow
alternatives of different lengths inside lookbehind, as long as the
length of each alternative is constant. They can handle something like
‹(?<=one|two|three|forty-two|gr[ae]y)
›, but
nothing more complex than that.
Internally, PCRE and Ruby 1.9 expand this into six lookbehind
tests. First, they jump back three characters to test ‹one|two
›, then four characters
to test ‹gray|grey
›,
then five to test ‹three
›, and finally nine to test ‹forty-two
›.
Java takes lookbehind one step further. Java allows any
finite-length regular expression inside lookbehind. This means you can
use anything except the infinite quantifiers ‹*
›, ‹+
›, and ‹{42,}
› inside lookbehind. Internally, Java’s
regex engine calculates the minimum and maximum length of the text
that could possibly be matched by the part of the regex in the
lookbehind. It then jumps back the minimum number of characters, and
applies the regex in the lookbehind from left to right. If this fails,
the engine jumps back one more character and tries again, until either
the lookbehind matches or the maximum number of characters has been
tried.
If all this sounds rather inefficient, it is. Lookbehind is very convenient, but it won’t break any speed records. Later, we present a solution for JavaScript and Ruby 1.8, which don’t support lookbehind at all. This solution is actually far more efficient than using lookbehind.
The regular expression engine in the .NET Framework is the only one in the world[5] that can actually apply a full regular expression from right to left. .NET allows you to use anything inside lookbehind, and it will actually apply the regular expression from right to left. Both the regular expression inside the lookbehind and the subject text are scanned from right to left.
If you use lookbehind at the start of the regex or lookahead at the end of the regex, the net effect is that you’re requiring something to appear before or after the regex match, without including it in the match. If you use lookaround in the middle of your regular expression, you can apply multiple tests to the same text.
In Flavor-Specific Features (a subsection of Recipe 2.3), we showed how to use character class subtraction to match a Thai digit. Only .NET and Java support character class subtraction.
A character is a Thai digit if it is both a Thai character (any sort) and a digit (any script). With lookahead, you can test both requirements on the same character:
(?=p{Thai})p{N}
Regex options: None |
Regex flavors: PCRE, Perl, Ruby 1.9 |
This regex works only with the three flavors that support Unicode scripts, as we explain in Recipe 2.7. But the principle of using lookahead to match the same character more than once works with all flavors discussed in this book.
When the regular expression engine searches for ‹(?=p{Thai})p{N}
›, it starts by
entering the lookahead at each position in the string where it begins
a match attempt. If the character at that position is not in the Thai
script (i.e., ‹p{Thai}
›
fails to match), the lookahead fails. This causes the whole match
attempt to fail, forcing the regex engine to start over at the next
character.
When the regex reaches a Thai character, ‹p{Thai}
› matches. Thus, the
‹(?=p{Thai})
›
lookaround matches, too. As the engine exits the lookaround, it
restores the match-in-progress. In this case, that’s the zero-length
match before the character just found to be Thai. Next up is ‹p{N}
›. Because the lookahead
discarded its match, ‹p{N}
› is compared with the same character that
‹p{Thai}
› already
matched. If this character has the Unicode property Number
, ‹p{N}
› matches. Since ‹p{N}
› is not inside a lookaround, it consumes
the character, and we have found our Thai digit.
When the regular expression engine exits a lookaround group, it discards the text matched by the lookaround. Because the text is discarded, any backtracking positions remembered by alternation or quantifiers inside the lookaround are also discarded. This effectively makes lookahead and lookbehind atomic. Recipe 2.14 explains atomic groups in detail.
In most situations, the atomic nature of lookaround is irrelevant. A lookaround is merely an assertion to check whether the regex inside the lookaround matches or fails. How many different ways it can match is irrelevant, as it does not consume any part of the subject text.
The atomic nature comes into play only when you use capturing groups inside lookahead (and lookbehind, if your regex flavor allows you to). While the lookahead does not consume any text, the regex engine will remember which part of the text was matched by any capturing groups inside the lookahead. If the lookahead is at the end of the regex, you will indeed end up with capturing groups that match text not matched by the regular expression itself. If the lookahead is in the middle of the regex, you can end up with capturing groups that match overlapping parts of the subject text.
The only situation in which the atomic nature of lookaround can alter the overall regex match is when you use a backreference outside the lookaround to a capturing group created inside the lookaround. Consider this regular expression:
(?=(d+))w+1
Regex options: None |
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby |
At first glance, you may think that this regex would match
123x12
.
‹d+
› would capture
12
into the
first capturing group, then ‹w+
› would match 3x
, and finally ‹1
› would match 12
again.
But that never happens. The regular expression enters the
lookaround and the capturing group. The greedy ‹d+
› matches 123
. This match is stored
into the first capturing group. The engine then exits the lookahead,
resetting the match-in-progress to the start of the string, discarding
the backtracking positions remembered by the greedy plus but keeping
the 123
stored
in the first capturing group.
Now, the greedy ‹w+
› is attempted at the start of the string. It
eats up 123x12
. ‹1
›, which references 123
, fails at the end of the string.
‹w+
› backtracks one
character. ‹1
› fails
again. ‹w+
› keeps
backtracking until it has given up everything except the first
1
in the
subject. ‹1
› also fails
to match after the first 1
.
The final 12
would match
‹1
› if the regex engine
could return to the lookahead and give up 123
in favor of 12
, but the regex engine doesn’t do
that.
The regex engine has no further backtracking positions to go to.
‹w+
› backtracked all
the way, and the lookaround forced ‹d+
› to give up its backtracking positions. The
match attempt fails.
<b>Kw+(?=</b>)
Regex options: Case insensitive |
Regex flavors: PCRE 7.2, Perl 5.10 |
Perl 5.10, PCRE 7.2, and later versions, provide an
alternative mechanism to lookbehind using ‹K
›. When
the regex engine encounters ‹K
› in the
regular expression, it will keep the text it has
matched so far. The match attempt will continue as it would if the regex
did not include the ‹K
›. But
the text matched prior to the ‹K
› will
not be included in the overall match result. Text matched by capturing
groups before the ‹K
› will
still be available to backreferences after the ‹K
›. Only
the overall match result is affected by ‹K
›.
The result is that ‹K
› can be
used instead of positive lookbehind in many situations. ‹beforeKtext
› will match
text
but only
when immediately preceded by before
, just as ‹(?<=before)text
› does. The benefit of
‹K
› over
positive lookbehind in Perl and PCRE is that you can use the full
regular expression syntax with ‹K
›, while
lookbehind has various restrictions, such as not allowing
quantifiers.
The major difference between ‹K
› and
lookbehind is that when you use ‹K
›, the
regex is matched strictly from left to right. It does not look backwards
in any way. Lookbehind does look backward. This difference comes into
play when the part of the regex after the ‹K
› or
after the lookbehind can match the same text as the part of the regex
before the ‹K
› or
inside the lookbehind.
The regex ‹(?<=a)a
› finds two matches in the string
aaa
. The first
match attempt at the start of the string fails, because the regex engine
cannot find an a
while looking back. The match attempt
starting between the first and second a
is successful. Looking back the regex
engine sees the first a
in the string, which satisfies the
lookbehind. The second ‹a
›
in the regex then matches the second a
in the string. The third match attempt
starting between the second and third a
is also successful. Looking back the
second a
in
the string satisfies the lookbehind. The regex then matches the third
a
. The final
match attempt at the end of the string also fails. Looking back the
third a
in the
string does satisfy the lookbehind. But there are no characters left in
the string for the second ‹a
› in the regex to match.
The regex ‹aKa
›
finds only one match in the string. The first match attempt at the start
of the string succeeds. The first ‹a
› in the regex matches the first a
in the string. ‹K
›
excludes this part of the match from the result that will be returned,
but does not change the matching process. The second ‹a
› in the regex then matches the
second a
in the
string, which is returned as the overall match. The second match attempt
begins between the second and third a
in the string. The first ‹a
› in the regex matches the third
a
in the string.
‹K
›
excludes it from the overall result, but the regex engine continues
normally. But there are no characters left in the string for the second
‹a
› in the regex to match,
so the match attempt fails.
As you can see, when using ‹K
›, the
regex matching process works normally. The regex ‹aKa
› will find the exact same
matches as the capturing group in the regex ‹a(a)
›. You cannot use ‹K
› to
match the same part of the string more than once. With lookbehind, you
can. You can use ‹(?<=p{Thai})(?<=p{Nd})a
› to match an
a
that is
preceded by a single character that is both in the Thai script and is a
digit. If you tried ‹p{Thai}Kp{Nd}Ka
› you’d be matching a Thai
character followed by a digit followed by an a
, but returning only the a
as the match. Again, this
is no different from matching all three characters with ‹p{Thai}p{Nd}(a)
› and using only
the part matched by the capturing group.
All the preceding arcane explanations are of no use if you’re using Ruby 1.8 or JavaScript, because you cannot use lookbehind at all. There’s no way to solve the problem as stated with these regex flavors, but you can work around the need for lookbehind by using capturing groups. This alternative solution also works with all the other regex flavors:
(<b>)(w+)(?=</b>)
Regex options: Case insensitive |
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby |
Instead of using lookbehind, we used a capturing group for the
opening tag ‹<b>
›.
We also placed the part of the match we’re interested in, the ‹w+
›, into a capturing
group.
When you apply this regular expression to My <b>cat</b> is
furry
, the overall regex match will be <b>cat
. The first
capturing group will hold <b>
, and the second, cat
.
If the requirement is to match only cat
(the word between the <b>
tags) because you want to extract
only that from the text, you can reach that goal by simply storing the
text matched by the second capturing group instead of the overall
regex.
If the requirement is that you want to do a search-and-replace, replacing only the word between the tags, simply use a backreference to the first capturing group to reinsert the opening tag into the replacement text. In this case, you don’t really need the capturing group, as the opening tag is always the same. But when it’s variable, the capturing group reinserts exactly what was matched. Recipe 2.21 explains this in detail.
Finally, if you really want to simulate lookbehind, you can do so
with two regular expressions. First, search for your regex without the
lookbehind. When it matches, copy the part of the subject text before
the match into a new string variable. Do the test you did inside the
lookbehind with a second regex, appending an end-of-string anchor
(‹z
› or
‹$
›). The anchor makes
sure the match of the second regex ends at the end of the string. Since
you cut the string at the point where the first regex matched, that
effectively puts the second match immediately to the left of the first
match.
In JavaScript, you could code this along these lines:
var mainregexp = /w+(?=</b>)/; var lookbehind = /<b>$/; if (match = mainregexp.exec("My <b>cat</b> is furry")) { // Found a word before a closing tag </b> var potentialmatch = match[0]; var leftContext = match.input.substring(0, match.index); if (lookbehind.exec(leftContext)) { // Lookbehind matched: // potentialmatch occurs between a pair of <b> tags } else { // Lookbehind failed: potentialmatch is no good } } else { // Unable to find a word before a closing tag </b> }
[5] RegexBuddy’s regex engine also allows a full regex inside
lookbehind, but does not (yet) have a feature similar to .NET’s
RegexOptions.RightToLeft
to reverse the
whole regular expression.