Understanding Regular Expressions By Mike McGrath

Written by In Easy Steps Team on . Posted in Article

A “Regular Expression” is simply a pattern describing a particular string of characters. Regular Expressions are often referred to as “regex” or “regexp” and are supported by most programming languages, such as C++, and scripting languages such as JavaScript. Regular Expressions are useful for text validation and for search-and-replace operations within text by matching their specified pattern to a section of the text.

A Regular Expression pattern may consist entirely of “literal characters” describing a particular character string to match within some text. For example, the Regular Expression ind finds a match in windows – the pattern literally matches the string within the text.

More typically a Regular Expresssion pattern consists of a combination of literal characters and “metacharacters” to describe a particular character string. Metacharacters have special meaning within the pattern rather than any literal meaning. For instance, the character ^ (circumflex or caret) is a metacharacter that has special meaning – it DOES NOT literally represent the actual caret character. It can control at which position in the text the match will be considered valid.

Most languages also support Regular Expression “Abbreviations” for frequently used Character Classes and Position expressions.

Here are 10 frequently used Regular Expressions:

1. Leading whitespace: ^\s+
2. Trailing whitespace: \s+$
3. Numbers from 0 to 999999: ^\d$
4. Valid HTML hexadecimal color values: ^#([a-fA-F0-9])(([a-fA-F0-9]))?$
5. U.S. Zip Code: ^\d(-\d)?$
6. U.S. Currency: ^\$(\d(\,\d)*|\d+)(\.\d)?$
7. U.S. Social Security Number: ^\d-\d-\d$
8. Date as MM/DD/YYYY HH:MM:SS: ^\d\d\/\d\d\/\d\d\d\d \d\d:\d\d:\d\d$
9. Email Address: ^[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z_+])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]$
10. HTTP URL: (https?):\/\/([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+([a-zA-Z])(:\d)?([-\w\/#~:.?+=&%@~]*)

About the author

Mike McGrath now lives in South-east Europe, on the sun-kissed shores of the Aegean Sea. Mike gained his extensive knowledge of computer languages while working as a developer contracting to companies around the world. His interests include coins of ancient Greece, dining-out with friends, and the ongoing evolution of the world wide web.

Tags: , , , , ,