Regular Expressions: Practical Examples

Regular Expressions are either a Developer’s dream or his worst nightmare. Most of the time, when I had to look at regular expressions, I considered them as some sort of black box that “do something” with a string, may it be either replace something in the string with something else or check if it matches a certain pattern. They can, indeed, get quite complicated and because of the fact I don’t use them very often, I decided to make some notes here. So I can look it up for the certain occasions when I have to deal with Regular Expressions.

Hands-on examples always help me more than long-winded introductions. So below you will find several examples of how Regular Expressions can be used to solve certain problems, together with explanations of how the pattern works. The languages used are PHP and JavaScript.

Numeric Values

Check if a numeric value ends with 0 or 5, e.g. value = 10940515

PHP

preg_match("/^\d*[05]$/", $value)

JavaScript

value.match(/^\d*[05]$/)

Explanation

  • /: Delimiter
  • ^: Pattern must match from the beginning of the string
  • \d: any digit
  • *: unspecified number (of digits, it refers to the character used before)
  • [05]: class of characters (0 and 5)
  • $: This should be the end of the string
  • /: Delimiter

Credit Card Number

Check if submitted credit card number is a valid credit card pattern string, e.g. ccvalue = 7895-7777-3365-2178

PHP

preg_match("/^(\d{4}[\s\-]?){4}$/", $ccvalue)

JavaScript

ccvalue.match(/^(\d{4}[\s\-]?){4}$/)

Explanation

  • /: Delimiter
  • ^: Pattern must match form the beginning of the string
  • (: beginning of sub-pattern
  • \d: any digit
  • {4}: should occur 4 times
  • [\s\-]: class of characters (blank and dash, escaped with backslash)
  • ?: character of this class can occur once or no time.
  • {4}: Multiplicator (previous pattern should occur 4 times)
  • $: This should be the end of the string

Credit Card Expiration Date

Check credit card expiration date, e.g. expdate = 0410

PHP

preg_match("/^(\d{2})1[0-9]$/", $expdate)

JavaScript

expdate.match(/^(\d{2})1[0-9]$/)

Explanation

  • /: Delimiter
  • (: beginning of subpattern
  • \d: any digit
  • {2}: should occur 2 times
  • 1: character “1″
  • [0-9]: class of characters, digits between 0 and 9 (for year)

Extract File Extension

Determine the extension of a file, e.g. filename = pc10010.jpg

PHP

preg_match("/\.(\w+)$/", $filename, $match)

JavaScript

filename.match(/\.(\w+)$/)

Explanation

  • \.: dot (escaped)
  • (: beginning of subpattern
  • \w: any alphanumeric character, including underscore
  • +: preceding character must occur at least once
  • $: This should be the end of the string

Querystring parameter

Retrieving a certain querystring (qid) parameter from a URL, e.g. URL = ‘http://answers.yahoo.com/question/index?qid=20080920092032AARA419′

PHP

preg_match("/qid=(.*)/", $qstring, $match)

JavaScript

qstring.match(/qid=(.*)/)

Explanation

  • /: delimiter
  • qid=: the value we are looking for in the URL
  • (: beginning of subpattern
  • .: any character
  • *: zero or more times
  • ): end of subpattern

General RegEx Resources

PHP Resources

JavaScript Resources

Switch to our mobile site