How to make a regex
- Search the templates (try date, phone or price) and pick one, or switch to Build your own. Arrow keys move through the list.
- To build one, add steps in order: exact text, digits, letters, one of a list of words and so on, and say how many of each. Upper and lower case are ignored unless you untick it; tick Whole words only or Must be the whole line if you need them.
- Check the highlighted matches in your own text, then copy the regex as it is, for JavaScript or for Python, or open it in the regex tester.
Reading a regex
A regex is a small pattern language. Once you know a few pieces, most patterns read left to right:
\dis a digit and[A-Za-z]is a letter. Square brackets mean “one of these characters”.+means one or more,*any number including none,?optional, and{3}exactly three.^and$mark the start and end of the line.\bmarks the edge of a word.(?:cat|dog)means “cat or dog”. A backslash before a symbol, like\., means the symbol itself.
So \d{3}-\d{4} reads as three digits, a dash, then four digits.
Templates included
| Group | Templates |
|---|---|
| Contact and web | Email address, website URL, domain name, US and international phone numbers, IPv4 and IPv6 addresses, IP address with port, MAC address, URL query parameters, Markdown links |
| Dates and times | Dates in three number orders and with month names, ISO 8601 date and time, 12 and 24-hour times, times with seconds, years |
| Numbers and money | Whole, decimal and comma-grouped numbers, scientific notation, percentages, prices in dollars, pounds and euros, credit card number format, hex and binary numbers, Roman numerals |
| Codes and IDs | US ZIP, UK and Canadian postcodes, hex and RGB colors, UUIDs, semantic version numbers, Git commit hashes, ISBN-13, JSON Web Tokens, latitude and longitude |
| Text | Usernames, strong passwords, hashtags, @mentions, text in quotes or brackets, capitalized words, words in capitals, repeated words, non-ASCII characters |
| Code and files | Image file names, file extensions, Windows paths, HTML tags and comments, // code comments, URL slugs |
| Cleanup | Blank lines, spaces at line starts and ends, double spaces, repeated lines |