DevBox

What Is a Regular Expression? A Clear Guide to the Basic Syntax

DevBox Team · Last updated 2026-07-02

Want to generate directly? Try Regex Tester — fill in the form and get results instantly. Open tool →

What Is a Regular Expression

A regular expression (regex for short) is a “pattern” used to match text by rules. For example, if you want to find all email addresses in a piece of text, validate a phone-number format, or batch-replace dates—a regex can describe it in one line, without writing a pile of if statements.

It works in almost every programming language, editor, and command-line tool (grep, sed), making it a universal tool for working with text.

Basic Components

A regex is made up of literal characters and metacharacters (symbols with special meaning). For example, in \d{4}-\d{2}: \d means “a digit,” {4} means “repeat 4 times,” and - is an ordinary hyphen.

Common Metacharacters Quick Reference

SymbolMeaningExample
.Any single character (excludes newline by default)a.c matches abc, a1c
\dDigit 0-9\d\d matches 42
\wLetter, digit, or underscore\w+ matches hello_1
\sWhitespace (space/tab/newline)
[abc]Character class: any one inside the brackets[aeiou] matches any vowel
[^abc]Negation: not these characters[^0-9] matches non-digits
[a-z]Range[a-z] matches lowercase letters

Quantifiers (Repetition Count)

SymbolMeaning
*0 or more times
+1 or more times
?0 or 1 time (optional)
{n}Exactly n times
{n,}At least n times
{n,m}n to m times

Groups and Anchors

  • Group ( ): wraps a part so it can be repeated as a whole or extracted. E.g. (ab)+ matches ababab; (?<year>\d{4}) is a named group.
  • Alternation |: cat|dog matches cat or dog.
  • Anchors: ^ matches the start, $ matches the end, and \b matches a word boundary. E.g. ^\d+$ means “the entire string is digits.”

Common Pitfalls

  • Forgetting to escape: ., *, (, etc. are themselves metacharacters; to match them literally you must escape them as \., \*, \(.
  • Greedy matching: .* matches as much as possible by default, often “matching too much”; use the non-greedy .*? when needed.

Next Steps

FAQ

Are regular expressions the same across different languages?

The basic syntax (character classes, quantifiers, groups, anchors) is common across most languages such as JavaScript, Python, Java, and PHP; a few advanced features (such as certain lookarounds, \p{} properties, and named-group syntax) differ, so for complex cases it's best to test in the target language.

Do I have to memorize every symbol to learn regex?

No. First master the dozen or so most common metacharacters (. * + ? [] () ^ $ \d \w \s), and use an online tester to write and see the highlighting as you go—you'll get the hang of it quickly.

More about Regex Tester