Monday, April 16, 2007

Mastering Regular Expressions, 3rd Edition

Book cover

So, after reading positive reviews about it since I started using regular expressions a little over a year ago, I finally purchased O'Reilly Media's Mastering Regular Expressions by Jeffrey E. F. Friedl, after discovering that a third edition came out in August 2006. The book arrived today, and it is indeed pretty damn excellent (I'm as excited about it as I can be about a tech book, anyway). I've only spent a few minutes with it so far, but I can see that it is excellently presented, and there is much to discover. Hopefully I'll post about some cool things I learn over the next few weeks, if I get a chance to actually sit down and read through a significant portion of the book.

Thursday, April 05, 2007

Faking Conditionals in Regular Expressions

Update: This post contains several major errors. Please view the updated version on my new blog:

Mimic Regular Expression Conditionals.

Excited by the fact that I can mimic atomic groups when using most regex libraries which don't support them, I set my sights on another of my most wanted regex features which is commonly lacking: conditionals (which provide an if-then-else construct). Of the libraries I'm familiar with, conditionals are only supported by .NET, PCRE, PHP (when using PCRE via the preg functions), and JGSoft products (including RegexBuddy).

There are two kinds of regex conditionals in those libraries... lookaround-based and capturing-group-based. The functionality of lookaround-based conditionals is very easy to replicate. First, here's what such conditionals look like (this example uses a positive lookahead for the assertion):

(?(?=if_assertion)then|else)

To mimic that behavior in languages which don't support conditionals, just add a colon after the initial question mark to turn it into a non-capturing group, like so:

(?:(?=if_assertion)then|else)

As long as the regex engine you're using supports the specified lookaround type, those patterns do the same thing.

However, mimicking capturing-group-based conditionals proved to be more tricky. Conditionals which use an optional capturing group as their test allow you to base logic on whether a capturing group has participated in the match so far. Thus...

(a)?b(?(1)c|d)

...matches only "bd" and "abc". That pattern can be expressed as follows:

(if_matched)?inner_pattern(?(1)then|else)

Here's a comparable pattern I created which doesn't require support for conditionals:

(?=(a)()|())\1?b(?:\2c|\3d)

To use it without an "else" part, you still need to include "\3" at the end, like this:

(?=(a)()|())\1?b(?:\2c|\3)

As a brief explanation of how that works, there's an empty alternation option within the lookahead at the beginning which is used to cancel the effect of the lookahead, while at the same time, the intentionally empty capturing groups within the alternation are exploited to base the then/else part on which option in the lookahead matched. However, there are a couple issues:

  • This doesn't work with some regex engines, due to how they handle backreferences for non-participating capturing groups.
  • It interacts with backtracking differently than a real conditonal (the "a" part is treated as if it were within an optional, atomic group... e.g., (?>(a)?) instead of (a)?), so it's best to think of this as a new operator which is similar to a conditional.

Here are the regex engines I've briefly tested this pattern with:

Language Supports "fake conditionals" Supports real conditionals Notes
.NET Yes Yes Tested using Expresso.
ColdFusion Yes No Tested using ColdFusion 7.
Java Yes No Tested using Regular Expression Test Applet.
JavaScript No No JavaScript assigns an empty string, rather than null, to backreferences for non-participating capturing groups. Unfortunately, this pattern depends on the way most other libraries handle non-participating capturing groups.
JGSoft Yes
(buggy)
Yes As of RegexBuddy version 2.3.2, it performs correctly in more cases if you change the two empty capturing groups ("()") to match a zero-length value of no impact, such as "(.{0})" or "(\b|\B)". It also has problems matching values at the end of a string when using an empty else. I've reported both issues to JGSoft, and have been told they will be fixed in the next version of RegexBuddy.
PHP Yes
(buggy)
Yes Tested using PHP Regex Tester. Performs correctly in more cases if you explicitly state the condition twice, like so: "(?=(?:a)()|())(?:a)?b(?:\1c| \2d)".

If you discover ways to improve this, or find problems not already mentioned, please let me know.

Tuesday, April 03, 2007

Faking Atomic Groups in Regular Expressions

So, I was messing around with RegexBuddy and discovered that capturing groups work inside lookarounds (e.g., "(?=(captured))"), even though, of course, lookarounds don't actually match anything. Consider that by using this technique, you can return text to your application (using backreferences) which wasn't contained within your actual match (backreference 0)!

Thinking back to the regex I just posted about (which matches innermost HTML elements, supporting an infinite amount of nesting), I realized this technique could actually be used to fake an atomic grouping. So, I've added a note on the end of the last post with an improved non-atomic-group-reliant version, which sure enough is nearly identical in speed to the regex which uses a real atomic grouping.

Here's how it's done:

(?=(pattern to make atomic))\1

Basically, it uses a capturing group inside a positive lookahead (which captures but doesn't actually match anything, so the rest of the regex can't backtrack into it), followed by "\1" (the backreference you just captured), to act just like an atomic group. That appears to produce the exactly same result as "(?>pattern to make atomic)", but can be used in programming languages which don't support atomic groups or possessive quantifiers (assuming they do support positive lookaheads). I can now use such constructs in languages like JavaScript and ColdFusion, and I think that's pretty freaking cool.

Monday, April 02, 2007

Regexes in Depth: Matching Innermost HTML Elements

Update: Please view the updated, syntax-highlighted version of this post on my new blog:

Matching Innermost HTML Elements.

On a regular expression forum I visit every once in awhile, a user asked how he could match all innermost tables within HTML source code. In other words, he wanted to match all tables which did not contain other tables. The regex should match "<table>...</table>", but should only match the inner table within "<table>...<table>...</table>...</table>". This logic needed to support an unlimited amount of nested tables.

One of the resident regex experts quickly claimed that regexes are not suited for parsing nested HTML data, and that this was therefore impossible using regular expressions, period.

It's true that, unless you're working with .NET or Perl, regexes are incapable of recursion (although it's often possible to fake it to an acceptable level). However, when people make claims like that, it encourages me to try to prove otherwise. ;-)

Here's the solution I offered (though there were a few steps to get there):

<table(?:\s[^>]*)?>(?:(?>[^<]+)|<(?!table(?:\s[^>]*)?>))*?</table>

That matches all innermost (or deepest level) tables, and supports an unlimited amount of nesting. It's also very fast, and it can easily be modified to work with other HTML elements (just change the three instances of "table" to whatever element name you want).

To demonstrate, the above regex matches the highlighted text below:

<table><td><table><td>&nbsp;</td></table></td></table> <table><tr><td>&nbsp;</td></tr></table> <table></table>

In order to explain how it works, I'll show the progression of gradually more solid regexes I tried along the way to the final result. Here was my first stab at the regex, which is probably easiest to follow (note that it's somewhat flawed):

<table>(?:[\S\s](?!<table>))*?</table>

Basically, the way that works is it matches an opening "<table>" tag, then it looks at each following character one at a time and checks if they are followed by another instance of "<table>" before "</table>". If so, the match fails, because it's not an innermost table.

In theory, at least. Within a couple minutes I realized there was a slight flaw. In order for it to work, there must be at least one character before it encounters a nested table (e.g., "<table>1<table></table></table>" has no problem, but "<table><table></table></table>" would return incorrect results). This is easily fixable by using another negative lookahead immediately after the opening "<table>", but in any case this regex is also slower than it needs to be, since it tests a negative lookahead against every character contained within table tags.

To address both of those issues, I used the following regex:

<table>(?:[^<]+|<(?!table>))*?</table>

First, that increases speed (in theory... you'll see that there are problems with this as is), because within each <table> tag it will greedily jump between all characters which are not "<" in single steps (using "[^<]+"), and it will only use the negative lookahead when it encounters "<". Secondly, it solves the previously noted error by using "<(?!table>)" instead of ".(?!<table>)".

If you're wondering about table tags which contain attributes, that's not a problem. The construct is such that it can easily be extended to support element attributes. Here's an updated regex to accomplish this (the added parts are highlighted in yellow):

<table(?:\s[^>]*)?>(?:[^<]+|<(?!table(?:\s[^>]*)?>))*?</table>

At first I thought this closed the case... The regex supports an unlimited amount of recursion within its context, despite the traditional wisdom that regexes are incapable of recursion. However, one of the forum moderators noted that its performance headed south very quickly when run against certain examples of real world data. This was a result of the regex triggering catastrophic backtracking. Although this is something I should've anticipated (nested quantifiers should always warrant extra attention and care), it's very easy to fix using an atomic grouping or possessive quantifier (I'll use an atomic grouping here since they're more widely supported). The change to the regex is highlighted:

<table(?:\s[^>]*)?>(?:(?>[^<]+)|<(?!table(?:\s[^>]*)?>))*?</table>

And that's it. As a result of all this, the regex not only does its job, but it performs quite impressively. When running it over a source code test case (which previously triggered catastrophic backtracking) containing nearly 100,000 characters and lots of nested tables, it correctly returned all innermost tables in less than 0.01 second on my system.

However, note that neither possessive quantifiers nor atomic groupings are supported by some programming languages, such as JavaScript. If you want to pull this off in JavaScript, a solid approach which is not susceptible to catastrophic backtracking would be:

<table(?:\s[^>]*)?>(?!<table(?:\s[^>]*)?>)(?:[\S\s](?!<table(?:\s[^>]*)?>))*?</table>

That runs just a little bit slower than (but produces the same result as) the earlier regex which relied on an atomic grouping.

If you have a copy of RegexBuddy (and if you don't, I highly recommend it), run these regexes through RegexBuddy's debugger for an under-the-hood look at how they're handled by a regex engine.

Edit: Using a trick I just stumbled upon (which I'll have to blog about in a second), the regex can be rewritten in a way that does not rely on an atomic grouping but is nearly as fast as the one that does:

<table(?:\s[^>]*)?>(?:(?=([^<]+))\1|<(?!table(?:\s[^>]*)?>))*?</table>

Basically, that uses a capturing group inside a positive lookahead followed by "\1" to act just like an atomic group!