Regular Expression Shortcuts

Regular expressions provide a powerful language to define patterns that can be used for data matching and searching operations.

In my last two articles about regular expressions (Fun With Regular Expressions and More Fun With Regular Expressions), I covered the basics. If you tried practicing on those basics, you'll find regular expressions very useful already. In fact, they are almost enough. It is safe to say that the basics are universally the same in all flavors of regular expressions.

Shorthands are best appreciated if you know and understand the basics. Choosing to use the shorthands is convenient when readability and maintainability become concerns. Remember, regular expressions are very linear and can grow very long to describe complex rules. Shorthands can help simplify codes. Note, however, that different flavors of regular expressions may have differences on how shorthands are interpreted.

In order to match a digit, you can use /[0-9]/ or the shorthand /\d/. In order to match a word character, you can use /[A-Za-z0-9_]/ or the short hand /\w/. In order to match whitespace characters, you can use /[ \t\r\n\f]/ or the shorthand /\s/.

Let's say for example that you want to match a five digit number. Given the knowledge you have so far, you may write /\d\d\d\d\d/. There is a special quantifier syntax that can make this look simpler. It has the format {min,max}, where min is the minimum number of times that the preceding expression can repeat and max is the maximum number of times that the preceding expression can repeat. Thus, in order to match a five digit number, the regular expression can instead be written as /\d{5,5}/.

Given a number like "9,753,124.680", where the decimal part is optional, the regular expression that would match this and other numbers like it can look something like this: /^(\d{1,3})(,\d{3,3})*(\.\d+)?$/. Crazy looking, right? But, what if the number is in the middle of a sentence. How should the regular expression be written? Give your solution a try at https://regex101.com/.

Have fun!

Comments