Improving Rexpy
Posted on Thu 09 March 2017 in TDDA
Today we are announcing some enhancements to Rexpy, the tdda tool for finding regular expressions from examples. In short, the new version often finds more precise regular expressions than was previously the case, with the only downside being a modest increase in run-time.
Background on Rexpy is available in two previous posts:
- This post introduced the concept and Python library
- This post discussed the addition of coverage information—statistics about which how many examples each regular expression matched.
Rexpy is also available online at https://rexpy.herokuapp.com.
Weaknesses addressed
Rexpy is not intended to be an entirely general-purpose tool: it is specifically focused on the case of trying to find regular expressions to characterize the sort of structured textual data we most often see in database and datasets. We are very interested in characterizing things like identifiers, zip codes, phone numbers, URLs, UUIDs, social security numbers and (string) bar codes, and much less interested in characterizing things like sentences, tweets, programs and encrypted text.
Within this focus, there were some obvious shortcomings of Rexpy, a significant subset of which the current release (tdda version 0.3.0) now addresses.
Example 1: Postcodes
Rexpy's tests have always included postcodes, but Rexpy never did a very good job with them. Here is the output from using tdda version 0.2.7:
$ python rexpy.py
EH1 1AA
B2 8EA
^[A-Z0-9]{2,3}\ [0-9A-F]{3}$
Rexpy's result is completely valid, but not very specific. It has correctly identified that there are two main parts, separated by a space, and that the first part is a mixture of two or three characters, each a capital letter or a number, and that the second part is exactly three characters, again all capital letters or numbers. However, it has failed to notice that the first group starts with a letter and follows this with a single digit and that the second group is one digit followed by two letters. What a human would probably have written is something more like:
^[A-Z]{1,2}[0-9]\ [0-9][A-F]{2}$
Let's try Rexpy 0.3.0.
^[A-Z]{1,2}\d\ \d[A-Z]{2}$
Now Rexpy does exactly what we would probably have wanted it to do,
and actually written it slightly more compactly—\d
is any digit,
i.e. it is precisely equivalent to [0-9]
.
With a few more examples, it still does the perfect thing (in 0.3.0).
$ rexpy
EC1 1BB
W1 0AX
M1 1AE
B33 8TH
CR2 6XH
DN55 1PT
^[A-Z]{1,2}\d{1,2}\ \d[A-Z]{2}$
(Note that the 0.3.0 release of TDDA includes wrapper scripts, rexpy
and tdda
that allow the main functions to be used directly from
command line. These are installed when you pip install tdda
.
So the rexpy
above is exactly equivalent to running python rexpy.py
.)
We should note, however, that it still doesn't work perfectly if we include general London postcodes (even with 0.3.0):
$ rexpy
EC1A 1BB
W1A 0AX
M1 1AE
B33 8TH
CR2 6XH
DN55 1PT
^[A-Z0-9]{2,4}\ \d[A-Z]{2}$
In this case, the addition of the final letter in the first block for
EC1A
and W1A
has convinced the software that the first block is just
a jumble of capital letters and numbers. We might hope that examples
like these (at least, if expanded) would result in something like:
^[A-Z]{1,2}\d{1,2}A?\ \d[A-Z]{2}$
though the real pattern for postcodes is actually
quite complex,
with only certain London postal areas being allowed a trailing letter,
and only in cases where there is a single digit in the first group,
and that letter can actually be A
, C
, P
or W
.
So while it isn't perfect, Rexpy is doing fairly well with postcodes now.
Let's look at another couple of examples.
Example 2: Toy Examples
Looking at the logs from the Rexpy online, it is clear that a lot of people (naturally) start by trying the sorts of examples commonly used for teaching regular expressions. Here are some examples motivated by what we tend to see in logs.
First, let's try a common toy example in the old version of Rexpy (0.2.7):
$ python rexpy.py
ab
abb
abbb
abbbb
abbbbb
abbbbbb
^[a-z]+$
Not so impressive.
Now in the new version (0.3.0):
$ rexpy
ab
abb
abbb
abbbb
abbbbb
abbbbbb
^ab+$
That's more like it!
Example 3: Names
Here's another example it's got better at. First under the old version:
$ python rexpy.py
Albert Einstein
Rosalind Franklin
Isaac Newton
^[A-Za-z]+\ [A-Za-z]{6,8}$
Again, this is not wrong, but Rexpy has singularly failed to notice the pattern of capitalization.
Now, under the new version:
$ rexpy
Albert Einstein
Rosalind Franklin
Isaac Newton
^[A-Z][a-z]+\ [A-Z][a-z]{5,7}$
Better.
Incidentally, it's not doing anything special with the first character of groups. Here are some related examples:
$ rexpy
AlbertEinstein
RosalindFranklin
IsaacNewton
^[A-Z][a-z]+[A-Z][a-z]{5,7}$
Example 4: Identifiers
Some of the examples we used previously were like this (same result under both versions):
$ rexpy
123-AA-22
576-KY-18
989-BR-93
^\d{3}\-[A-Z]{2}\-\d{2}$
What worked less well in the old version were examples like these:
$ python rexpy.py
123AA22
576KY18
989BR93
^[A-Z0-9]{7}$
These work much better under the 0.3.0:
$ rexpy
123AA22
576KY18
989BR93
^\d{3}[A-Z]{2}\d{2}$
Some Remaining Areas for Improvement
The changes that we've made in this release of Rexpy appear to be almost unambiguous improvements. Both from trying examples, and from understanding the underlying code changes, we can find almost no cases in which the changes make the results worse, and a great number where the results are improved. Of course, that's not to say that there don't remain areas that could be improved.
Here we summarize a few of the things we still hope to improve:
Alternations of Whole Groups
Rexpy isn't very good at generating alternations at the moment, either
at a character or group level. So for example, you might have hoped
that in the following example, Rexpy would notice that the middle two
letters are always AA
or BB
(or, possibly, that the letter is
repeated).
$ rexpy
123-AA-321
465-BB-763
777-AA-81
434-BB-987
101-BB-773
032-BB-881
094-AA-662
^\d{3}\-[A-Z]{2}\-\d{2,3}$
Unfortunately, it does not. (This probably won't be very hard to change.)
Alternations within Groups
Similarly, you might hope that it would do rather better than this:
$ rexpy
Roger
Boger
Coger
^[A-Z][a-z]{4}$
Clearly, we would like this to produce
^[A-Z]oger$
and you might think from the previous examples that it would do this,
but it can't combine the fixed oger
with the adjacent letter range.
Too Many Expressions (or Combining Results)
Rexpy also produces rather too many regular expressions in many cases, particularly by failing to use optionals when it could. For example:
$ rexpy
Angela Carter
Barbara Kingsolver
Socrates
Cher
Martin Luther King
James Clerk Maxwell
^[A-Z][a-z]+$
^[A-Z][a-z]{5,6}\ [A-Z][a-z]+$
^[A-Z][a-z]{4,5}\ [A-Z][a-z]{4,5}\ [A-Z][a-z]+$
At least in some circumstances, we might prefer that this would produce a single result such as:
^[A-Z][a-z]+((\ [A-Z][a-z]+(\ [A-Z][a-z]+)?)?$
or, ever better:
^[A-Z][a-z]+(\ [A-Z][a-z])*$
Although we would definitely like Rexpy to be able to produce one of these results, we don't necessarily always want this behaviour. It transpires that in a TDDA context, producing different expressions for the different cases is very often useful. So if we do crack the "combining" problem, we'll probably make it an option (perhaps with levels); that will just leave the issue of deciding on a default!
Plans
We have ideas on how to address all of these, albeit not perfectly, so expect further improvements.
If you use Rexpy and have feedback, do let us know. You can reach us on Twitter at (@tdda0), and there's also a TDDA Slack (#TDDA) that we'd be happy to invite you to.