Use our free tool — no sign-up required.

Regex Tester & Validator — Free Online Tool

What is a regular expression?

A regular expression is a sequence of characters that defines a search pattern. You write the pattern, and a regex engine scans through text to find strings that match it. The pattern can be as simple as a literal word — error matches the word "error" anywhere in a string — or as complex as a full email or URL validator spanning dozens of characters. Regex engines are built into virtually every modern programming language (JavaScript, Python, Java, PHP, Go, Ruby) and many text editors and command-line tools like grep and sed.

The key insight is that regex patterns are composed of two kinds of characters: literals (characters that match themselves, like a, B, 7) and metacharacters (special characters with reserved meanings, like . * + ? ^ $ {} [] () and |). Learning what each metacharacter does is the foundation of reading and writing regex.

Essential metacharacters and what they do

Here is a quick reference for the most frequently used regex building blocks:

How to write and test a regex — step by step

Building a regex from scratch is easiest when you follow a methodical process:

  1. Step 1: Define what you want to match. Write down two or three sample strings that should match and two or three that should not. For example, for a US phone number you might want to match 555-867-5309 and (555) 867-5309 but not 123 or not a phone.
  2. Step 2: Break the pattern into parts. A US phone number has an optional area code in parentheses, followed by a 3-digit exchange, a separator, and a 4-digit number. Map each part to a regex token: (\(\d{3}\)\s?)?\d{3}[-.\s]\d{4}.
  3. Step 3: Test against your samples in the Regex Tester. Paste your pattern and test string into the tool. Watch which parts match and which do not. Most regex testers highlight matches in real time and show capture group contents separately — use this to diagnose issues.
  4. Step 4: Refine and add anchors. If your pattern is for validation (the entire string must match, not just a substring), wrap it in ^ and $ anchors. Add flags as needed — the i flag for case-insensitive matching, g to find all matches rather than just the first.

Common regex patterns for everyday tasks

Tips and best practices

Frequently asked questions

What does the dot (.) metacharacter mean in regex?

A dot matches any single character except a newline by default. The pattern c.t matches "cat", "cut", "cot", and even "c5t". If you want to match a literal dot — as in a file extension or domain name — you must escape it: \.. So file\.txt matches only "file.txt" and not "fileXtxt". The dot is one of the most common sources of accidental over-matching in regex, so always ask yourself whether you want any character or a literal dot.

What is the difference between + and * quantifiers?

The asterisk (*) means "zero or more occurrences" of the preceding element. The plus sign (+) means "one or more occurrences." For example, go*gle would match "ggle", "gogle", "google", and "gooooogle". But go+gle only matches "gogle", "google", and longer versions — not "ggle", because + requires at least one occurrence. Use + when the element must appear at least once, and * when it is optional.

What are regex flags and how do I use them?

Flags change how a regex engine interprets a pattern. The three most common are: g (global) — find all matches instead of stopping after the first; i (case insensitive) — treat uppercase and lowercase as identical; m (multiline) — make ^ and $ match the start and end of each line rather than the entire string. In JavaScript, flags appear after the closing slash: /pattern/gi means global and case-insensitive.

How do I write a regex to validate an email address?

A practical email regex for most use cases is: ^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$. The local part before @ allows letters, digits, dots, underscores, percent signs, plus signs, and hyphens. After the @ comes the domain name and a dot followed by at least two letters for the TLD. Note that no single regex can perfectly validate email per the full RFC spec — use this to catch obvious formatting errors and confirm via a verification email for critical applications.

Conclusion

Regular expressions reward investment — a few hours of learning metacharacters and quantifiers unlocks a capability you will use for the rest of your career. Start with simple patterns, test them incrementally in a live tester, and build up complexity only as needed. The free Regex Tester highlights matches in real time and shows capture groups, making it the fastest way to go from an idea to a working pattern without switching between tools.

Ready to build your regex? Try our free tool — works instantly in your browser.

Regex Tester & Validator — Try it Free

Related tools