Tuesday, 13 May 2008

flex regular expressions

I really like how you can inline regular expressions in Flex. For instance:

var split:Array = someString.split(/\./);

which will of course split "someString" using "." as a delimeter. Of course, all that's really happening is that the regular expression is being converted to an object by the compiler, so the following is the same:

var regexp:RegExp = /\./;
var split:Array = someString.split(regexp);

This is superiour to Java's approach which allows you to specify a regular expression as a String when calling split, the problem being that the above regexp would look like this:

String regExp = "/\\./";

Note how I have to escape the escape character!

Of course, Java has a whole package for regular expression handling, but they all suffer the same problem of having to encode the regular expression in a string and thus having to escape the escape characters... which makes long regular expressions very complicated and as a result I suspect a lot of Java developers simply avoid using regular expressions for that reason. In my view inlining regular expressions is definately a candidate for being added to the core Java language.


Anton said...

good RegExp tester at http://flexregexptest.narod.ru/

Ryan Swanson said...

I just posted a new application called the Flex 3 Regular Expression Explorer and thought you might be interested in it.


It is similar in style to the Flex Component Explorer or Styles Explorer in that you can select from a library of examples and edit and test a regular expression directly within the application.

I took the tool even further, though, by adding the ability for anyone to publish their own regular expression examples to a 'community' section.

I also added a full featured help panel since so many people have difficulty understanding and using regular expressions.

Hope you like it!