Modern regular expression engines have the ability to “look around”. They can look ahead or look behind to see whether some text can be matched at the position the regex engine has reached in the subject string, without actually including that text in the regex match. This makes it possible to check whether the match is preceded or followed by certain text, or whether it is not preceded or followed by something.
To demonstrate this, we’ll create a regular expression that matches text between <b> tags in an HTML file. That text would appear bold in a web browser. The regex will match only the text between the tags. It will check for the presence of the tags without including them in the regex match. You can find this example as “Fields: lookaround” in the RegexMagic library.
The example uses the Match panel to build up the regular expression in a logical way, rather than from left to right. First we make the regex match the text we want. Then we add the requirement for that text to be preceded by an opening <b> tag and followed by a closing </b> tag.
Some <b>bold</b> text. And <i>italics</i>. We need to <b>match</b> this. Ignore <b>mismatched</i> <i>tags</b>.
button to add field
.
to match one or more characters.
button again to insert a new field
before the existing field, which changes its number to
.
. When you do this, RegexMagic adds a new field
as the pattern for the lookaround. The original field changes its number to
.
.
, set “pattern to match field” to “literal text”.
.
to make the field buttons work on that field.
to add field
after field
.
. RegexMagic adds field
as the pattern for the lookaround.
.
, set “pattern to match field” to “literal text”.
.
# 1. Look behind to assert that fields 2 to 2 can be matched ending at this position (?<= # 2. Literal text <b> ) # 3. Basic characters [^<>]+ # 4. Look ahead to assert that fields 5 to 5 can be matched starting at this position (?= # 5. Literal text </b> )Required options: Case insensitive; Free-spacing.
Some <b>bold</b> text. And <i>italics</i>. We need to <b>match</b> this. Ignore <b>mismatched</i> <i>tags</b>.
This example only puts one field inside each lookaround. But you can add as many fields as you like. They form a sequence inside the lookaround.
Many regular expression flavors impose restrictions on lookbehind. Some only allow literal text inside lookbehind. Some allow alternatives of fixed length. A few allow quantifiers with an upper bound. Very few allow infinite quantifiers. The reason for this is that lookbehind requires the regular expression to work backwards. Only the JavaScript, .NET, and JGsoft regex engines support true right-to-left matching.
RegexMagic allows any of its patterns inside lookbehind. But when you generate the regular expression you may get an error that the pattern you’re trying to use doesn’t work inside lookbehind because of the limitations of your application’s regex engine.
Older regular expression flavors do not support lookbehind or even lookahead at all. With a traditional regular expression, to match text between HTML tags, the regex match needs to include the HTML tags. You can add a capturing group to retrieve the text between the tags. If the above example results in an error when you try to generate the regex for your preferred application, then you can modify it as follows:
to “a single pattern”. This effectively deletes field
. Field
which matches <b> becomes the new field
. The other fields also have their numbers decremented by one.
. Change its “kind of field” to “a single pattern” too. This removes the lookahead and field
becomes the new field
.
basic characters” in the “field” drop-down list. We now have a capturing group that will give us the text matched by field
.# 1. Literal text <b> # 2. content: Basic characters (?P<content>[^<>]+) # 3. Literal text </b>Required options: Case insensitive; Free-spacing.
Some <b>bold</b> text. And <i>italics</i>. We need to <b>match</b> this. Ignore <b>mismatched</i> <i>tags</b>.
match = re.search( """# 1. Literal text <b> # 2. content: Basic characters (?P<content>[^<>]+) # 3. Literal text </b>""", subject, re.VERBOSE | re.IGNORECASE) if match: result = match.group("content") else: result = ""