What is a regular expression?
A regular expression (regex) is a pattern that describes a set of strings. You use them to search, validate, or extract text that matches a specific structure — email addresses, phone numbers, dates, URLs, or any pattern you can describe.
Using the ToolHive regex tester
Enter your pattern in the top field — without the surrounding slashes, the tester adds those for you. Type your test string in the lower textarea. Matches are highlighted in yellow in real time as you type. The matches panel below shows each match with its position and any capture groups.
The flag buttons control how the regex engine interprets the pattern. The g (global) flag is enabled by default and finds all matches rather than stopping after the first. The i flag makes matching case-insensitive. The m flag makes ^ and $ match the start and end of each line rather than the entire string. The s flag makes the dot (.) match newlines as well as other characters.
Basic pattern elements
The dot (.) matches any single character except a newline. A character class like [aeiou] matches any one of the listed characters. A negated class [^aeiou] matches any character that is not in the list. \d matches any digit (0–9), \w matches any word character (letters, digits, underscore), and \s matches any whitespace character.
Quantifiers control how many times an element matches. The asterisk (*) means zero or more. The plus (+) means one or more. The question mark (?) means zero or one. Curly braces {n,m} specify a minimum and maximum count.
Capture groups
Parentheses create capture groups that extract specific parts of a match. The pattern (\d{4})-(\d{2})-(\d{2}) matches a date like 2024-03-15 and captures the year, month, and day in three separate groups. The tester shows each group's content alongside the full match.
Non-capturing groups (?:...) group elements without creating a capture — useful for applying quantifiers to a group without the overhead of storing the match.
Anchors
The caret (^) anchors the pattern to the start of the string (or line with the m flag). The dollar ($) anchors to the end. Without anchors, the pattern can match anywhere in the string — with them, it must match at the specified position. This matters for validation: the pattern \d+ matches "abc123def" (finding the digits), but ^\d+$ only matches if the entire string is digits.
Lookaheads and lookbehinds
Lookaheads assert that a match is followed (or not followed) by something, without including that something in the match. (?=...) is a positive lookahead: \w+(?=\s*:) matches a word that is followed by a colon. (?!...) is a negative lookahead. Lookbehinds (?<=...) and (?
Common regex patterns
Email validation: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ — note that truly validating email requires an RFC 5322 parser, but this covers 99% of real addresses. URL matching: https?://[^\s]+ — matches http:// or https:// followed by non-whitespace. IPv4 address: (\d{1,3}\.){3}\d{1,3} — matches four groups of 1–3 digits separated by dots.