A r t i c l e s
Navigation

Note: This site is
a bit older, personal views
may have changed.

M a i n P a g e

D i r e c t o r y

TRegExpr Considered Harmful


TRegExpr for delphi, and regexpr2 for freepascal needs to be optimized or debugged.

I converted a similar unit to TRegExpr (from lazarus/synedit) to work with freepascal on linux and win32 and called it regexpr2, but since it is derived from TRegexpr, it is just as slow as TRegExpr. This unit is laughingly slow on even small files. Anything more than a 300 line file will peg the CPU on even the simplest regular expression.

Something is severely wrong with the unit which Andrey has created for us. It is packed with features, a complete Regex unit.. with modifiers and the whole shabang. But the speed is terrible. On a 10 line file it works fine.. and for small one time 5 line operations it works fine. But anything more than a few lines and the CPU pegs for hours.

TRegexpr is a unit from a site called "RegexpStudio".

If I find the bugs in his code, I'll update the units for freepascal and post here what was the bottleneck.

After contacting the author, he said it wasn't designed for checking text files or anything like that, it was just for small jobs. I'm not sure if there is a new version of the regex studio out there now or what has gone in in the past few years with TRegexp. Personally I try to only use regular expressions in emergency since most regexes can be replaced with good parsers or functions like IsNum IsAlphaNumeric, IsEmailAddress, etc.

See also

Above units give you a hint on how to write maintainable functions that don't use any regular expressions. Of course regular expressions are kind of useful for whipping up quick hacks, but in some cases I strongly recommend using functions like the above ones instead of regexes. Abusing regexes makes pascal code ugly like perl/php, and it makes code less maintainable, and it means you have to add heavy verbose source comments to each and every regex you build. Sometimes, a function is just better, even if a function is initially harder to write. See the IsEmail function for example, which isn't something that is too trivial, but at the same time it isn't that hard.

About
This site is about programming and other things.
_ _ _