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
- preg_match function
- preg_match_all function
- preg_replace function
- preg_split function
- Regular Expressions PHP Tutorial
- Using Regular Expressions with PHP
- PHP Regular Expressions Examples
- PHP regular expression tutorial
JavaScript Resources
- match() function (German)
- replace() function (German)
- search() function (German)
- ReExp Object (German
- Using Regular Expressions with JavaScript
Categories
- California (3)
- Cars (1)
- Cologne (3)
- Cycling (7)
- Design (1)
- Events (10)
- Gadgets (1)
- Hockey (1)
- Job (12)
- Life in General (19)
- London (7)
- Mobile (8)
- Music (3)
- Guitar (3)
- NYC (11)
- Programming (6)
- Radio (1)
- Shopping (1)
- Travel (21)
- Uncategorized (1)
- Video (1)
- Web (56)
- Coding (37)
- Agent YUI (6)
- Performance (5)
- Design (2)
- Coding (37)
Blogroll
- Andy Clarke
- Stuart Colville
- Mike West
- Ben Ward
- Tom Hughes-Croucher
- Dave Shea
- Tim Huegdon
- Jeremy Keith
- Marianne Masculino
- Rachel Andrew
Recent Posts
- Boost the performance of your mobile web app using YUI Compressor
- Cycling through Death Valley, powered by REI
- Bike ride up (and down) a volcano
- Text to speech functionality in web applications using the iSpeech API
- Search along a route with CloudMade’s Local Search API