Regular Expressions Are the Best! s/Best/Worst/
Regular Expressions. Ya love 'em or ya hate 'em. But it shouldn't be so black-or-white. Here's when they do, and don't make sense.Regular Expressions. Regex.
Ya love ’em or ya hate ’em, it seems.
In some languages (Perl anyone?) they’re so central to the language, that it’s almost impossible to imagine life without them. In others, they’re tacked on as a cumbersome library.
So should you use regular expressions? Should you avoid them? Does it depend on your language of choice?
I have five simple rules I like to use to help me decide whether or not to use regular expressions:
- Handling HTML, XML, email addresses, or any other input with complex or irrelgular parsing rules? Don’t use regex
- Is there a library or function to handle parsing/validation/search for you? Don’t use regex
- Are you parsing regular, but complex input, that would be tedious to parse without a regular expression? Maybe use regex
- Is your search/replace pattern provided by a user? Likely use regex
I’ve written up a much longer explanation of the thought process behind this list, too, if you’re interested.