Regular expression examples (common regex patterns)

Regular expression FAQ: Do you have a list of common regular expression examples, i.e., the most common regex patterns used in programming languages like Java, Perl, PHP, Ruby, and others?

I've been asked several times, so I thought it might be helpful to post some of the most common regular expression examples that are used in programming. Honestly I haven't tested any of these yet -- I did them off the top of my head -- but I think they will work for most open source programming languages.

Regular Expression What it Matches
^$ A blank line.
[A-Z][A-Z] Two character state abbreviation.
Note that you may choose to put spaces either before or after the regex.
.*, [A-Z][A-Z] City, State abbreviation.
[0-9]\{5\}(-[0-9]\{4\})? Zip Code.
Five digits, followed by an optional hyphen and four additional digits.
[0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\} Social security number, such as:
\$[0-9]*.[0-9][0-9] Dollar amounts, specified with a leading $ symbol.
[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} Date, in numeric format, such as 2003-08-06.
[A-Z][a-z][a-z] [0-9][0-9]*, [0-9]\{4\} Date, in format such as "Jan 3, 2003"

I'll add more regular expression examples as I think of them, but these are some of the first that come to mind. I'm sure that other common regular expressions can easily be extrapolated from these. Or, if you have some that you want to share, just use the Comments form below.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.