Every day O’Reilly has an “ebook deal of the day” offering one or a bunch of their books in electronic format for only $9.99. Twice this year I received an email from O’Reilly notifying me that Regular Expressions Cookbook was on sale. But each time the email was sent on the morning of the day itself. When it’s morning in California it’s already bedtime for me here in Thailand. So I never saw the emails until the next day, making it rather pointless to blog about the deal.
But this time O’Reilly has listened to my request for advance notification. I just got an email this morning saying Regular Expressions Cookbook will be part of the Ebook Deal of the Day for 15 September 2010. That’s 15 September on the US west coast. When I write this there’s a few hours to go before the deal starts at one past midnight California time. You can get any O’Reilly Cookbook as an ebook for only $9.99. The normal price for Regular Expressons Cookbook as an ebook is $31.99. The download includes the book in PDF, ePub, Mobi (for Kindle), DAISY, and Android formats.
The RegularExpressionsCore unit that is part of Delphi XE is based on the latest class-based PerlRegEx unit that I developed. Embarcadero only made a few changes to the unit. These changes are insignificant enough that code written for earlier versions of Delphi using the class-based PerlRegEx unit will work just the same with Delphi XE.
- The unit was renamed from PerlRegEx to RegularExpressionsCore. When migrating your code to Delphi XE, you can choose whether you want to use the new RegularExpressionsCore unit or continue using the PerlRegEx unit in your application. All you need to change is which unit you add to the uses clause in your own units.
- Indentation and line breaks in the code were changed to match the style used in the Delphi RTL and VCL code. This does not change the code, but makes it harder to diff the two units.
- Literal strings in the unit were separated into their own unit called RegularExpressionsConsts. These strings are only used for error messages that indicate bugs in your code. If your code uses TPerlRegEx correctly then the user should not see any of these strings.
- My code uses assertions to check for out of bounds parameters, while Embarcadero uses exceptions. Again, if you use TPerlRegEx correctly, you should never get any assertions or exceptions. The
Compile method raises an exception if the regular expression is invalid in both my original TPerlRegEx component and Embarcadero’s version. If your code allows the user to provide the regular expression, you should explicitly call Compile and catch any exceptions it raises so you can tell the user there is a problem with the regular expression. Even with user-provided regular expressions, you shouldn’t get any other assertions or exceptions if your code is correct.
Note that Embarcadero owns all the rights to their RegularExpressionsCore unit. Like all the other RTL and VCL units, this unit cannot be distributed by myself or anyone other than Embarcadero. I do retain the rights to my original PerlRegEx unit which I will continue to make available for those using older versions of Delphi.
The new RegularExpressionsCore unit in Delphi XE is based on the PerlRegEx unit that I wrote many years ago. Since I donated full rights to a copy rather than full rights to the original, I can continue to make my version of TPerlRegEx available to people using older versions of Delphi. I did make a few changes to the code to modernize it a bit prior to donating a copy to Embarcadero. The latest TPerlRegEx includes those changes. This allows you to use the same regex-based code using the RegularExpressionsCore unit in Delphi XE, and the PerlRegEx unit in Delphi 2010 and earlier.
If you’re writing new code using regular expressions in Delphi 2010 or earlier, I strongly recomment you use the new version of my PerlRegEx unit. If you later migrate your code to Delphi XE, all you have to do is replace PerlRegEx with RegularExrpessionsCore in the uses clause of your units.
If you have code written using an older version of TPerlRegEx that you want to migrate to the latest TPerlRegEx, you’ll need to take a few changes into account.
The original TPerlRegEx was developed when Borland’s goal was to have a component for everything on the component palette. So the old TPerlRegEx derives from TComponent, allowing you to put it on the component palette and drop it on a form.
The new TPerlRegEx derives from TObject. It can only be instantiated at runtime. If you want to migrate from an older version of TPerlRegEx to the latest TPerlRegEx, start with removing any TPerlRegEx components you may have placed on forms or data modules and instantiate the objects at runtime instead. When instantiating at runtime, you no longer need to pass an owner component to the Create() constructor. Simply remove the parameter.
Some of the property and method names in the original TPerlRegEx were a bit unwieldy. These have been renamed in the latest TPerlRegEx. Essentially, in all identifiers SubExpression was replaced with Group and MatchedExpression was replaced with Matched. Here is a complete list of the changed identifiers:
| Old Identifier |
New Identifier |
StoreSubExpression |
StoreGroups |
NamedSubExpression |
NamedGroup |
MatchedExpression |
MatchedText |
MatchedExpressionLength |
MatchedLength |
MatchedExpressionOffset |
MatchedOffset |
SubExpressionCount |
GroupCount |
SubExpressions |
Groups |
SubExpressionLengths |
GroupLengths |
SubExpressionOffsets |
GroupOffsets |
Download TPerlRegEx. Source is included under the MPL 1.1 license.
Delphi XE and C++Builder XE will ship with regular expression support built right into the runtime library. The RegularExpressions unit is based on code originally written by Vincent Parrett. It implements a set of classes (records, actually) that mimics the regex classes in the .NET framework. That includes the whole set of convenient static methods as well as automatic cleanup when the record goes out of scope.
The RegularExpressions unit uses the RegularExpressionsCore unit to do the heavy lifting. The RegularExpressionsCore unit is a modified version of the PerlRegEx unit developed by yours truly. This unit defines a single Delphi class called TPerlRegEx that wraps the functionality of the PCRE open source library, with added methods for replacing and splitting. You can use the RegularExpressionsCore unit directly, just as you can already use the PerlRegEx unit in older versions of Delphi.
Vincent and I donated copies of our code to Embarcadero. We keep the full rights to our code, while Embarcadero gets full rights to their copy. That means I can continue to distribute TPerlRegEx for older versions of Delphi. I’ll release a new version of TPerlRegEx that includes some changes I made prior to donating the code to Embarcadero.
I’ll blog about these two units with more technical details once RAD Studio XE ships. The next free minor updates of RegexBuddy and RegexMagic will support Delphi XE and X++Builder XE using these new units, while retaining support for TPerlRegEx for older versions of Delphi.