Using Regular Expressions – Part 3 of 3 – Examples

Following on Geekology’s previous posts on Regular expressions (Regex Overview and Regex in PHP), the following is a list of regular expression examples for general use.

Credit Cards:

All major credit cards:

^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$

American Express:

^3[47][0-9]{13}$

Diners Club International:

^3(?:0[0-5]|[68][0-9])[0-9]{11}$

Discover:

^6011[0-9]{12}$

MasterCard:

^5[1-5][0-9]{14}$

Visa:

^4[0-9]{12}(?:[0-9]{3})?$

Dates:

d/m/yy or dd/mm/yyyy:

\b(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)?[0-9]{2}\b

dd/mm/yyyy:

(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)[0-9]{2}

m/d/y or mm/dd/yyyy:

\b(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2}\b

mm/dd/yyyy:

(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2}

yy-m-d or yyyy-mm-dd:

\b(19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])\b

yyyy-mm-dd:

(19|20)[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])

Email Addresses:

Email Address:

\b[A-Z0-9._%-+]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

Email Address (anchored):

^[A-Z0-9._%-+]+@[A-Z0-9.-]+\.[A-Z]{2,4}$

Email Address (anchored, no consecutive dots):

^[A-Z0-9._%-+]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$

Email Address (no consecutive dots):

\b[A-Z0-9._%-+]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b

Email Address (specific TLDs):

^[A-Z0-9._%-+]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|biz|info|name|aero|biz|info|jobs|museum|name)$

HTML:

HTML Comment:

<!--.*?-->

HTML File:

.*?.*?.*?.*?.*?]*>.*?.*?

HTML File (atomic):

(?>.*?)(?>.*?)(?>.*?)(?>.*?)(?>.*?]*>)(?>.*?).*?

HTML Tag:

<([A-Z][A-Z0-9]*)[^>]*>(.*?)<!--\1-->

HTML Tag (without contents):

<!--?[a-z][a-z0-9]*[^<-->]*>

IP Addresses:

IP Addresses:

\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

IP Addresses (stored in capturing group):

\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

Logs:

Apache HTML Hits:

^((?#client IP or domain name)\S+)\s+((?#basic authentication)\S+\s+\S+)\s+\[((?#date and time)[^]]+)\]\s+"(?:GET|POST|HEAD) ((?#file)/[^ ?"]+?\.html?)\??((?#parameters)[^ ?"]+)? HTTP/[0-9.]+"\s+(?#status code)200\s+((?#bytes transferred)[-0-9]+)\s+"((?#referrer)[^"]*)"\s+"((?#user agent)[^"]*)"$

Apache 404 Errors:

^((?#client IP or domain name)\S+)\s+((?#basic authentication)\S+\s+\S+)\s+\[((?#date and time)[^]]+)\]\s+"(?:GET|POST|HEAD) ((?#file)[^ ?"]+)\??((?#parameters)[^ ?"]+)? HTTP/[0-9.]+"\s+(?#status code)404\s+((?#bytes transferred)[-0-9]+)\s+"((?#referrer)[^"]*)"\s+"((?#user agent)[^"]*)"$

Numbers:

Currency Amount:

\b[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?\b

Floating Point:

[-+]?\b[0-9]*\.?[0-9]+\b

Hexadecimal:

\b0[xX][0-9a-fA-F]+\b

Programming:

Comment (#):

#.*$

Comment (//):

//.*$

Comment (/* */):

/\*.*?\*/

URLs:

Slug:

/^[a-z0-9-]+$/

URL:

/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/

 

Related posts:

  1. Using Regular Expressions – Part 2 of 3 – Regex in PHP
  2. Using Regular Expressions – Part 1 of 3 – Overview
  3. Extract and sort email addresses from text files with Automator
  4. Checking your internal and external IP Addresses on a Unix machine
  5. A simpler way to send Text or HTML emails with attachments in PHP
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

3 Responses to “Using Regular Expressions – Part 3 of 3 – Examples”

  1. Those email regex-s don’t allow subaddressing (that means “address+subaddress@mail.com”). Subaddressing is one really good feature – so you have disposable emails. I’m always frustrated when I want to use and can’t.

    It also gives feedback where did the email leak to spammers.

  2. Your email regex is too restrictive, there are valid email addresses which it won’t match. I’d normally use a more permissive part for the bit before @ such as \S+@

  3. Hey guys

    Good point, thanks!

    I’ve now added that in.

Afrigator