Skip to content

Regular Expressions Quick Reference

A regular expression (also known as regex or regexp) is a special sequence of characters that defines a search pattern.

Example References

Description Regex Example Match Example Compatibility
Locate "foo" and "bar". ^(?s)(?=.*foo)(?=.*bar) Bar said "foo how are you doing today"? PCRE
String starts with 79 and has a given date/time. ^79|([a-zA-Z]+).*?\|2013-12-20 12:30:00 79|eng|813100|||TVPG|L|2013-12-20 12:30:00|30|0|0| |1|0|0|0|1|0|0|0||0|0|0|0|0|0|||0|0|0|0 Notepad++ (POSIX)
String contains " (Mapped to: " and ends with a line feed (\n). (Mapped to: .*?\n ATMAX (Mapped to: MAXLAT Notepad++ (POSIX)
Boundary Character Class (case in-sensitive) /\bthe\b/i The new music channel. PHP, PCRE
Camel Case ^[a-z]+(?:[A-Z][a-z]+)*$
Email Address /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ foobar@example.com PHP
Hostname ^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$
IPv4 Address ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
Postal Code /^[a-z]\d[a-z] ?\d[a-z]\d$/i S7H 0A1 PHP
Longitude /^[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/ -1.21321 PHP
Latitude /^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?)$/ -12.213213 PHP
Longitude/Latitude /^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?);[-]?((((1[0-7][0-9])|([0-9]?[0-9])\.(\d+))|180(\.0+)?)$/ -12.213213;-1.21321 PHP. More Info
Multiple White space Characters /\s+/ How are you today? PHP, PCRE
NIC MAC Address with Delimiters /^[0-9a-fA-F]{2}(?=([:;.]?))(?:\\1[0-9a-fA-F]{2}){5}$/ 1C:6F:65:34:A7:34 PHP, PCRE
NIC MAC Address without Delimiter (case in-sensitive) /^[0-9a-fA-F]{12}$/ 1C6F6534a734 PHP, PCRE
One and Two Character Words /(?<!\S)\S{1,2}(?!\S)/ He is the one who walks a line. PHP, PCRE
Network IP v4 Address /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/ 127.0.0.1 PHP, PCRE
Pascal Case /^[A-Z][a-z0-9]*(?:[A-Z][a-z0-9]*)$/
Punctuation and Symbols /[[:punct:]]/ He's doing fine. How are you? PHP, PCRE
Semantic Version ^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(\\-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$
UUID ^[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}\Z b80a121a-51a6-11e5-a734-b827eb8df375 Python, PCRE
USA Zip Code with optional dash/last 4 digits /^[0-9]{5}(-[0-9]{4})?$/ 12345 PHP, PCRE
USA Zip Code 5 or 9 digit with dash /^\d{5}([\-]\d{4}){0,1}$/ PHP, PCRE

In-depth Explanation

NIC MAC Address Explanation with Delimiters

^ => Match the beginning of the string

[0-9a-fA-F]{2} => Match the hexadecimal value

(?=([:;.]?)) => Lookahead (?=) and try to match the first delimiter (:;.). Storing the value matched in a backreference. 
You'll note the [:;.] is inside "()" to capture the matched value. The "?" means matching the [:;.] is optional.

(?:\\1[0-9a-fA-F]{2}){5} => surrounding a pattern in () means group and capture the matched pattern in a backreference. 
The ?: following the opening "(" means don't capture the pattern matched. Only group it.

The \\1 holds the value of the 1st backreference. In this case the delimiter. We try to match the delimiter we first 
matched then a hexadecimal value 5 times ({5}).

$ => matches the end of the string.

Universally Unique Identifier (UUID)

Searching for UUIDs in text with regex