Tuesday, October 18

Regular Expressions

I've so far managed to avoid using regular expressions, preferring to use code to validate input data. Unfortunately, I had to use them yesterday, as a software package we use can be needs to use them for input validation.

I had to ensure a field contained exactly four characters if it was entered, but it could contain nothing at all. It wasn't easy to find out what I should use to verify a field contained nothing, but following a lot of trial and error, the following seemed to work:

([0-9a-zA-Z]{4})|^$

I think I should learn more about these, and how you can verify things like email addresses.

I've ordered the book, Sams Teach Yourself Regular Expressions in 10 Minutes by Ben Forta, which was only £5.49, and has been well reviewed. Amazon say they have dispatched it, so it should arrive today or tomorrow. I'll let you know 10 minutes after I get it if it's any good.

No comments: