A regular expression (regex) is a sequence of characters that defines a search pattern. Think of it as a powerful "find" tool that can match not just exact text, but patterns of text โ like "any email address" or "all phone numbers in this document."
Regex is used everywhere: form validation, search-and-replace in code editors, log file analysis, data extraction, and web scraping. Learning regex is one of the highest-leverage skills for developers, data analysts, and power users.
At its simplest, a regex is just literal text. The pattern hello matches the word "hello" anywhere in a string. But regex becomes powerful with special characters:
. โ matches any single character except newline. h.t matches "hat", "hit", "hot"\d โ matches any digit (0-9). \d\d\d matches "123", "456"\w โ matches any word character (letter, digit, underscore)\s โ matches any whitespace (space, tab, newline)\D, \W, \S โ the negated versions (non-digit, non-word, non-space)Quantifiers specify how many of the preceding element to match:
* โ zero or more. ab*c matches "ac", "abc", "abbc", "abbbc"+ โ one or more. ab+c matches "abc", "abbc" but NOT "ac"? โ zero or one (optional). colou?r matches "color" and "colour"{3} โ exactly 3. \d{3} matches exactly three digits{2,5} โ between 2 and 5. \w{2,5} matches 2-5 word characters{3,} โ 3 or moreSquare brackets define a set of characters to match:
[aeiou] โ matches any vowel[a-z] โ matches any lowercase letter[A-Za-z0-9] โ matches any alphanumeric character[^0-9] โ the caret negates: matches anything that is NOT a digitParentheses create groups that can be captured and reused:
(abc) โ captures "abc" as a group(cat|dog) โ alternation: matches "cat" OR "dog"(\d{3})-(\d{4}) โ captures area code and number separately$1, $2, etc. to restructure text.^ โ start of string. ^Hello matches "Hello world" but not "Say Hello"$ โ end of string. world$ matches "Hello world" but not "world class"\b โ word boundary. \bcat\b matches "cat" but not "catalog"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Matches most standard email addresses like user@example.com.
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
Matches: (555) 123-4567, 555-123-4567, 5551234567, 555.123.4567
https?:\/\/[\w.-]+\.[a-z]{2,}[\/\w.-]*
Matches most HTTP and HTTPS URLs.
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
\d{2}\/\d{2}\/\d{4}
Try these in the 67Fresh Regex Tester: