Common Regular Expression Patterns
This section contains a list of commonly used regular expressions that can be used to identify or format data. This list is provided as an introduction to regular expressions. These and other patterns can be modified to suit your needs.
Note: The regular expressions below will work in many situations. However, the pattern you are trying to match may have extra white spaces, variations, or similar patterns that need to be accounted for. Always test your regular expressions on a variety of data and edit them as needed so that they only return the results you want.
Type | Pattern | Example |
---|---|---|
Phone number ((xxx) xxx-xxxx format) |
\(\d\d\d\) \d\d\d-?\d\d\d\d |
(562) 988-1688 (562) 9881688 |
Short date (single or double-digit month-day format) |
\d?\d/\d?\d/\d\d\d\d | 12/25/2009 |
Social Security number (xxx-xx-xxxx) |
\d\d\d-\d\d-\d\d\d\d |
123-45-6789 |
Time (h:mm or hh:mm format) |
[0-9]?[0-9]:[0-9][0-9] | 17:50 |
ZIP code | \d{5}-\d{4}|\d{5} | 90807 or 92064-3404 |
ID or tracking number ("Tracking ID" followed by a colon, space, and any combination of letters or numbers) |
Tracking ID\:\s*(\w*)\s* | Tracking ID: 12KDF7Q89WFL |
Legal Land Description (Two possible formats. Both have the format two letters-two numbers-three numbers-two numbers. Then, the first allows a single digit at the end. The second allows a letter and digit at the end.) |
[A-Z]{2}-\d{2}-\d{3}-\d{2}-[A-Z]\d|[A-Z]{2}-\d{2}-\d{3}-\d{2}-\d | NW-03-042-12-W4 |
Last Name, First Name (Allows for hyphens in the last name, and underscores in both names.) |
([\w\-]+)\s*,\s*(\w+)\s* | Smith, Paul |
Fiscal Year (October through September of the next year) |
1[0-2]/\d?\d/2015|0?[1-9]/\d?\d/2016 | Returns a date between 10/01/2015 and 9/31/2016 |
Parent folder from a folder path (Finds everything after the last backslash in a string) |
([^/\\]*)$ |
From \My Documents\Sales\Contracts returns Contracts |
The following table demonstrates the regular expressions that should be used to retrieve a single line of text from data containing multiple lines of text.
Note: The First Line pattern will not return the desired results when the data being processed only contains a single line of text.
Type | Pattern |
---|---|
First Line | ^[^\n]+ |
Any specific line, where N is the line number. Line numbers are zero-based. E.g., the first line is numbered 0, the second line is numbered 1, etc. | (?:[^\n]*\n){N}(.+) |
Last Line | [^\n]+$ |