Using Regular Expressions in 4D
Regular expressions are an extremely powerful tool for describing text. They're often included as a power user feature in many text editors and word-processors and many langauges and development environments have built-in support for them.
Regular expressions are a late entrant into the 4D family of products, and even now are only available through 3rd Party Plug-ins. Hopefully, 4D will address this and have native support built-in, but for the moment, the choices available for using regular expressions in 4D are fairly good.
This article will not teach the reader how to write regular expressions, but rather give some ideas of where and how regular expressions can be used with 4D. It will hopefully show readers new to regular expressions just how useful they can be and give an incentive to go and learn how to write them for themselves. I am by no means an expert on regular expressions, but I'm learning more all the time and I encourage those 4D developers who currently don't know how to use them to learn about them, especially if any of your work involves text parsing of any kind.
There is an excellent book on regular expressions entitled, "Mastering Regular Expressions" by Jeffrey E. F. Friedl, which is an absolute must for anybody who wants to use and eventually master regular expressions. It's the first book I've read on the subject which does a good job of explaining how to use regular expressions to someone who has never used them before. Again, I will not try and teach regular expression in this article, but will instead provide links where you can find more information for yourself.
Since discovering Regular Expressions (or Regex patterns) a year or so ago I have wondered how I managed to live so long without them. I now use them regularly when programming in PHP or 4D, but also for editing HTML and other source code. For example, using regular expressions, creating the hyperlinked 4D source code on this website was very much easier than you would think. Creating the index of all the 4D commands was done with 1 simple regex applied to each page in the index of the manual. Converting the code was also done with a series of simple regular expressions and some other find and replace operations to create the XML. Then the PHP XML process puts the two together and along with the stylesheet I get formatted 4D code with hyperlinks on the commands. Using regular expressions this took no time at all and is completely reusable.
Note: This article will remain a "work in progress" for a while as we find out new ways to entice 4D developers into using regular expressions. Expect fairly regular updates to this article, if you would like to contribute some ideas, I'd love to hear from you.
Planet 4D
The first part of a tutorial on using Regular Expressions aimed at 4D developers is available in Planete 4D Issue 7, the French journal for 4D developers. Visit the Planete 4D website to order your copy.
|