Data lookups
Regular-expression reference
Regex tokens, character classes, flags and worked examples.
43 entries.
| Token | Matches | Example |
|---|---|---|
| . | Any single character except a newline | a.c matches "abc", "a-c" |
| \d | Any digit 0–9 | \d\d matches "42" |
| \D | Any character that is not a digit | |
| \w | Word character: letter, digit or underscore | \w+ matches "hello_1" |
| \W | Any non-word character | |
| \s | Whitespace: space, tab, newline | |
| \S | Any non-whitespace character | |
| \b | Word boundary (zero width) | \bcat\b matches "cat" but not "category" |
| \B | Not a word boundary | |
| [abc] | Any one of a, b or c | [aeiou] matches any vowel |
| [^abc] | Any character except a, b or c | |
| [a-z] | Any character in the range | [A-Za-z0-9] alphanumeric |
| ^ | Start of the string (or line with /m) | ^Hello |
| $ | End of the string (or line with /m) | world$ |
| * | Zero or more of the preceding | ab*c matches "ac", "abc", "abbc" |
| + | One or more of the preceding | ab+c matches "abc" but not "ac" |
| ? | Zero or one — makes it optional | colou?r matches both spellings |
| {n} | Exactly n times | \d{4} matches a 4-digit year |
| {n,} | n or more times | \d{2,} |
| {n,m} | Between n and m times | \d{3,5} |
| *? +? ?? | Lazy versions — match as little as possible | <.+?> stops at the first ">" |
| | | Either side (alternation) | cat|dog |
| ( ) | Capturing group | (\d{4})-(\d{2}) captures year and month |
| (?: ) | Non-capturing group | (?:ab)+ |
| (?<name> ) | Named capturing group | (?<year>\d{4}) |
| \1 | Back-reference to group 1 | (\w)\1 matches a doubled letter |
| (?= ) | Positive lookahead | foo(?=bar) matches "foo" only before "bar" |
| (?! ) | Negative lookahead | foo(?!bar) |
| (?<= ) | Positive lookbehind | (?<=£)\d+ matches digits after a £ |
| (?<! ) | Negative lookbehind | |
| \ | Escape — treat the next character literally | \. matches a real full stop |
| flag i | Case-insensitive | /hello/i |
| flag g | Global — find all matches (JavaScript) | /a/g |
| flag m | Multiline — ^ and $ match line ends | /^x/m |
| flag s | Dot matches newlines too | /a.b/s |
| flag u | Unicode mode | /\p{L}/u |
| flag x | Extended — ignore whitespace and allow comments | |
| \p{L} | Any Unicode letter (needs u flag) | matches "é", "日" |
| Example: email | Rough email check | ^[^@\s]+@[^@\s]+\.[a-z]{2,}$ |
| Example: UK postcode | UK postcode, loose | ^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$ |
| Example: hex colour | CSS hex colour | ^#(?:[0-9a-f]{3}|[0-9a-f]{6})$ |
| Example: date | ISO date yyyy-mm-dd | ^\d{4}-\d{2}-\d{2}$ |
| Example: strip tags | An HTML tag | <[^>]+> |
Syntax shown is PCRE, used by PHP, JavaScript, Python and most editors. Test patterns before trusting them with real data.