Tech News, Tech & Coding · Free Online Tools

IT Question Answer
Tools
Regular Expressions for Beginners: Patterns, Examples and Testing

Regular Expressions for Beginners: Patterns, Examples and Testing

Developer ToolsBy Muhammad Fareed3 min read
regexregular-expressionstestingdeveloper-tools

A regular expression, or regex, is a pattern that describes text. It lets you search, validate and replace text in a fraction of the code you would need otherwise. Regex looks cryptic at first, but a small set of symbols covers most everyday tasks. This guide explains them with examples you can try in a regex tester.

The basic building blocks

SymbolMeaningExample
.Any character except a newlinec.t matches cat, cut
\dA digit\d\d matches 42
\wA letter, digit or underscore\w+ matches hello_1
\sA whitespace charactera\sb matches a b
[abc]One of a, b or cgr[ae]y matches gray, grey
^ and $Start and end of the text^Hello

Quantifiers: how many times

  • * zero or more, + one or more, ? zero or one.
  • {3} exactly three, {2,4} two to four.

Example: \d{3}-\d{4} matches 555-1234.

Groups and alternatives

  • Parentheses group parts: (ab)+ matches ab, abab.
  • The pipe means "or": cat|dog.
  • Groups can capture text to reuse in replacements.

Practical patterns

  • Simple email check: ^[^\s@]+@[^\s@]+\.[^\s@]+$. It catches obvious mistakes, but real email validation is complex, so also send a confirmation email.
  • Date like 2026-11-07: ^\d{4}-\d{2}-\d{2}$ checks the format only, not whether the date exists.
  • Hex color: ^#(?:[0-9a-fA-F]{3}){1,2}$.
  • Extra spaces: replace \s{2,} with a single space.

Test before you ship

Paste your pattern and sample text into the Regex Tester to see the matches highlighted. Test with normal examples, edge cases and text that should not match. Remember that flags such as g (global), i (ignore case) and m (multiline) change the result.

Common mistakes

  • Forgetting to escape special characters such as ., ? and ( when you mean them literally.
  • Greedy matching, where .* takes more text than you wanted. Use .*? for the shortest match.
  • Writing patterns that are too complex. Break them into smaller steps or add comments.
  • Catastrophic backtracking from nested quantifiers on untrusted input, which can freeze a program.

Practice exercises

  1. Match a five-digit postal code: ^\d{5}$.
  2. Find all words that start with a capital letter: \b[A-Z]\w*.
  3. Extract hashtags from a post: #\w+.
  4. Match a time like 14:30: ^([01]\d|2[0-3]):[0-5]\d$.

Try each in the tester with both matching and non-matching examples.

Key takeaways

  • Learn the core symbols first: character classes, quantifiers, anchors and groups.
  • Build patterns in small steps and test them with both matching and non-matching text.
  • Prefer simple patterns, and use a proper parser for complex formats such as HTML.

Frequently asked questions

Do all programming languages use the same regex syntax?

The basics are the same, but details differ between languages and engines, such as lookbehind support and named groups. Check the documentation for your language.

Is regex good for parsing HTML?

No. HTML is too complex for regex. Use an HTML parser for anything beyond a quick, simple extraction.

How do I learn regex faster?

Practice on real text: build patterns step by step in a tester, read the match highlights and solve small puzzles such as extracting dates or phone numbers.

Try Regex Tester & Generator free

Advertisement

Related posts