Regex Guru

Tuesday, 18 August 2009

Regular Expressions Cookbook on Safari Books Online

Filed under: Regex Cookbook — Jan Goyvaerts @ 14:32

If you have a Safari Books Online subscription, you can start reading Regular Expressions Cookbook on Safari Books Online right now. If you don’t, you can still click that link and then click the red free trial button in the upper right corner of the web page.

When Regular Expressions Cookbook was published as a printed book in June, it was immediately available on Safari in “print fidelity view”. This view loads one page from the printed book at a time as an image. I don’t find that view very useful for technical books. It requires too many clicks and downloads too slow to read comfortably. That’s why I didn’t blog about the Safari edition before.

Safari Books Online now shows Regular Expression Cookbook in HTML format. HTML format loads much faster even while displaying one complete recipe per web page, even if it spans a dozen pages in the printed book.

Monday, 17 August 2009

Kindle Edition of Regular Expressions Cookbook

Filed under: Regex Cookbook — Jan Goyvaerts @ 14:31

When Regular Expressions Cookbook was published, I was told by O’Reilly that a Kindle edition would be unlikely, due to the special formatting we use in the book for regular expressions. It seems that those technical difficulties have been overcome, as Amazon now offers a Kindle edition of Regular Expressions Cookbook. I presume the formatting of the Kindle edition won’t be identical to the printed book. I don’t have a Kindle, iPod, or iPhone, so I can’t check.

When I write this, Amazon lists the Kindle edition for $17.59. That’s the lowest price for the book I’ve seen to date. You need a Kindle, iPhone, or iPod Touch to be able to read the Kindle edition. Amazon sells the printed book for $29.70, while O’Reilly sells a DRM-free PDF for $31.99 and the printed book for $44.99.

Wednesday, 12 August 2009

Updated PCRE DLL for TPerlRegEx

Filed under: Regex Libraries — Jan Goyvaerts @ 16:44

Some users have reported that TPerlRegEx for Delphi 2009 causes their applications to crash when the component is freed. I finally got around to testing this in detail. It turns out the bug only occurs when using the DLL, and that the crash actually occurs in the DLL when pcre_free is called.

I’ve now released a new version of TPerlRegEx that comes with a new DLL that exports a custom pcre_dispose function which the component now calls instead of pcre_free. To upgrade to the new version, simply recompile your application with the new PerlRegEx.pas and pcre.pas, and replace pcre3.dll with pcrelib.dll. The new DLL is compiled from the PCRE 7.9 sources.

While I was at it, I also updated the OBJ files to PCRE 7.9. To use the OBJ files instead of the DLL, edit pcre.pas to comment out the PCRE_LINKDLL define and uncomment the PCRE_STATICLINK define. Due to bugs in the Delphi compiler, this only works if you do not install TPerlRegEx into a design time package. That means you’ll have to instantiate the component at runtime instead of dropping it onto a form.

Download TPerlRegEx. Source is included under the MPL 1.1 license.

Update: I hadn’t properly updated all the files in the .zip file on on August 12th. This has now been fixed.

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.

« Previous PageNext Page »