In Easy Steps Articles

December 7, 2011

Understanding Regular Expressions By Mike McGrath

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.

Sign up to our

Newsletter

Our newsletters inform you of new and forthcoming titles, handy tips, and other updates and special offers. You can opt out anytime.

"*" indicates required fields

By anniemcg

Share

We are sorry to let you go...

Would you mind giving us your feedback or reason of cancelling the subscription?

 

"*" indicates required fields

Hidden
Hidden
Hidden
Reason For Cancellation*
Hidden