Regex Guru

Monday, 27 July 2009

Magically Generating Regular Expressions

Filed under: The Guru's Kitchen — Jan Goyvaerts @ 14:23

A lot of people are looking for a program that can automatically generate regular expressions for them. The program would only take examples of valid matches as input, and produce the proper regular expression as output, inferring the user’s idea of “proper” as by magic. It is certainly one of the more frequent feature request I receive for RegexBuddy. Unfortunately, no computer program will ever be able to generate a meaningful regular expression based purely on a list of valid matches. Let me show you why.

Suppose you provide the examples 111111 and 999999. Which regular expression should the computer generate?

  1. A regex matching exactly those two examples: (?:111111|999999)
  2. A regex matching 6 identical digits (\d)\1{5}
  3. A regex matching 6 ones and nines [19]{6}
  4. A regex matching any 6 digits \d{6}
  5. Any of the above four, with word boundaries, e.g. \b\d{6}\b
  6. Any of the first four, not preceded or followed by a digit, e.g. (?<!\d)\d{6}(?!\d)

As you can see, there are many ways in which examples can be generalized into a regular expression. The only way for the computer to build a predictable regular expression is to require you to list all possible matches. Then it could generate a search pattern that matches exactly those matches, and nothing else. Usually, providing an exhaustive list of matches is exactly what we’re trying to avoid. And when you do have an exhaustive list of all possible matches, an optimized plain text search processing the whole list at once will be as fast as or faster than a regex search. The plain text search can be optimized to scan the text only once, without backtracking like regular expressions do

If you don’t want to list all possible matches, you need a higher-level description. Instead of providing a long list of 6-digit numbers, you simply tell the program to match “any six digits”. The regular expression syntax itself is one way to provide such a description. Regular expressions are powerful enough that they can describe any text that doesn’t depend on its context. “Any six digits” is written as \d{6} in regular expression syntax.

To make the higher-level description easy to work with, it needs domain knowledge. Matching a date between January 1st and March 31st is much easier if your tool or language knows what a date is. This is where regular expressions fall short. Regular expressions only know about characters. Essentially, a regular expression describes which character comes next, or which characters are allowed next.

This wouldn’t be the regex guru blog if there story ended here. Though pure auto-generation of regexes isn’t feasible, there have to be better ways of creating text patterns than spelling things out character by character. I don’t mean trying to make regular expressions more accessible by coming up with a more verbose syntax, as some people have tried. Microsoft would be pushing COBOL# instead of C# if wordiness was key.

I mean a tool that works at a higher level. Something that allows you to say “a number between 1 and 12″ instead of “a digit between 1 and 9, or a digit 1 followed by a digit between 0 and 2″. That’s what I’ve been working on for the past two years. It’s the reason why there haven’t been any major Just Great Software releases since 2007.

RegexMagic is a brand new product from Just Great Software. Its purpose is to make it easier to create regular expressions, but without using the regular expression syntax. You can tell RegexMagic you want a date between January 1st and March 31st, and that you want it in yyyy-mm-dd format, simply by selecting the “date and time” pattern and setting its options. Once you’ve done that, RegexMagic magically spits out your regular expression. Version 1.0 has patterns for characters, numbers, dates, times, email addresses, URLs, credit card numbers, national IDs, and more.

In practice, most of the regular expressions you want won’t neatly fit into one of RegexMagic’s predefined patterns. If you mark 1.2.12 as a whole, RegexMagic will guess it’s a date (1 February 2012, German style), rather than a product version number. If want a regex that matches 3 numbers delimited by dots, mark the numbers and the dots separately into 5 fields. Select the integer pattern for the numbers and the literal text pattern for the dots. Then RegexMagic can again magically spit out your regex, even if it didn’t magically read your mind.

Once you have created your higher-level description in RegexMagic, which is called a RegexMagic formula, editing and customizing that description is trivial compared to editing a regex. If you decide that the parts of a version should be restricted to values between 1 and 255, simply set the limits on the integer patterns, and regenerate the regular expression.

Just like RegexBuddy, RegexMagic supports nearly all popular regular expression flavors. Select your flavor, and RegexMagic makes sure to generate a regular expression that works with it. RegexMagic can also generate snippets in many programming languages that you can copy and paste directly into your source code to implement your regular expression. RegexMagic uses the same source code templates as RegexBuddy.

Though RegexMagic is mainly aimed at people new to regular expressions, experts on regular expressions may still find RegexMagic useful to quickly generate more complicated regexes. E.g. if you need to match numbers between 256 and 512, and all you can use is a single regex, it takes less than a minute to make RegexMagic spit out 51[0-2]|50[0-9]|[34][0-9]{2}|2[6-9][0-9]|25[6-9]. Though such a regex is conceptually very simple, it is quite a chore to build up by hand.

For more details, a trial download, and an introduction discount, please visit RegexMagic’s web site.

Wednesday, 10 June 2009

#1 In Computer Books on Amazon.com

Filed under: Regex Cookbook — Jan Goyvaerts @ 9:10

When I write this, Regular Expressions Cookbook is at the top spot in Amazon.com’s computers & internet bestsellers, its first day in the top 100. Its overall sales rank among all books sold by Amazon.com is #117.

Hitparades like that are very volatile. A book like ours that’s focused on a particular technical subject won’t stay at the top very long. So you’ll have to forgive me for tooting my own horn a bit after sacrificing several months of my life on this. (The whole process took a year, but Steve and I didn’t work on it full time.)

Jan Goyvaerts holding his author copy of Regular Expressions Cookbook

Friday, 29 May 2009

Regular Expressions Cookbook in PDF Format

Filed under: Regex Cookbook — Jan Goyvaerts @ 15:06

O’Reilly’s website lists Regular Expressions Cookbook in PDF format for $31.99. That’s less than the list price of $44.99 for the print version, but just a bit more than the discounted price of $29.69 that Amazon.com is asking for the print version right now. But if you want instant access or you live in the tropics where it takes 3 weeks and costs $10 to receive anything from Amazon, and you don’t have a Safari subscription, the PDF is a good deal.

Thursday, 28 May 2009

The True Story About The Rat

Filed under: Regex Cookbook — Jan Goyvaerts @ 11:29

I previously blogged about the rat on the cover of Regular Expressions Cookbook. Turns out it is not really a rat, if you get technical about such things. In fact, it’s not a rodent at all. The colophon in the book has this to day:

The image on the cover of Regular Expressions Cookbook is a musk shrew (genus Crocidura, family Soricidae). Several types of musk shrews exist, including white- and red-toothed shrews, gray musk shrews, and red musk shrews. The shrew is native to South Africa and India.

While several physical characteristics distinguish one type of shrew from another, all shrews share certain commonalities. For instance, shrews are thought to be the smallest insectivores in the world, and all have stubby legs, five claws on each foot, and an elongated snout with tactile hairs. Differences include color variations among their teeth (most noticeably in the aptly named white- and red-toothed shrews) and in the color of their fur, which ranges from red to brown to gray.

Though the shrew usually forages for insects, it will also help farmers keep vermin in check by eating mice or other small rodents in their fields.

Many musk shrews give off a strong, musky odor (hence their common name), which they use to mark their territory. At one time it was rumored that the musk shrew’s scent was so strong that it would permeate any wine or beer bottles that the shrew happened to pass by, thus giving the liquor a musky taint, but the rumor has since proved to be false.

The cover image is from Lydekker’s Royal Natural History.

« Previous PageNext Page »