Whenever I ask a group of developers if they are familiar with regular expressions, I seem to get at least half the responses along the lines of “I’ve used them, but I don’t like them”. Call me a geek if you like, but I quite like regex; I think often it seems unfriendly because it’s used inappropriately or just thrown into code with “here be dragons” type comments rather than documentation about what should match, and what shouldn’t!
As with most things, it’s pretty easy when you know how, so here’s my one-step-at-a-time approach to regex (stolen from my ZCE preparation tutorial slides). Let’s begin at the very beginning: regular expressions have delimiters, usually a slash character, and these contain a pattern that describes a string.
pattern |
notes |
/b[aeiou]t/ |
Matches “bat”, “bet”, “bit”, “bot” and “but”
Also matches “cricket bat”, “bitter lemon” |
Continue reading →