ConvertCaseTool
Tutorial7 min read

Regular Expressions (Regex) for Beginners: A Practical Guide

Regular expressions (regex) are patterns used to match, search, and manipulate text. They are one of the most powerful tools in a developer's toolkit, but they can look intimidating at first. This guide breaks regex down into simple, understandable pieces with practical examples you can test right away using our Regex Tester.

What Are Regular Expressions?

A regular expression is a sequence of characters that defines a search pattern. Think of it as a "find" command on steroids. While a normal text search looks for an exact string, regex can match patterns like "any email address" or "a phone number in any format."

Regex is supported in virtually every programming language (JavaScript, Python, Java, C#, Go, PHP, Ruby) and many text editors (VS Code, Sublime Text, Notepad++).

Basic Syntax

Literal Characters

The simplest regex is just a plain string. The pattern cat matches the text "cat" wherever it appears.

Metacharacters

CharacterMeaningExampleMatches
.Any character (except newline)c.tcat, cut, c9t
^Start of string^Hello"Hello world"
$End of stringworld$"Hello world"
\dAny digit (0-9)\d\d\d123, 456
\wAny word character\w+hello, test123
\sAny whitespacehello\sworld"hello world"

Quantifiers

QuantifierMeaningExampleMatches
*0 or moreab*cac, abc, abbc
+1 or moreab+cabc, abbc (not ac)
?0 or 1colou?rcolor, colour
{n}Exactly n times\d{3}123, 456
{n,m}Between n and m times\d{2,4}12, 123, 1234

Character Classes

Square brackets define a set of characters to match:

[abc] → matches a, b, or c

[a-z] → matches any lowercase letter

[A-Z0-9] → matches any uppercase letter or digit

[^abc] → matches anything EXCEPT a, b, or c

Common Patterns

Here are regex patterns you can use right away. Test them with our Regex Tester:

Email Address

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}

URL

https?://[\w.-]+(?:\.[a-zA-Z]{2,})(?:/[\w./-]*)?

Phone Number (US)

\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}

Practical Tips

  • Start simple: Build your pattern piece by piece, testing each step
  • Use a tester: Always test with our Regex Tester before using patterns in production code
  • Be specific: Avoid overly broad patterns that match more than intended
  • Escape special characters: Characters like . * + ? must be escaped with \ to match literally
  • Use flags: The i flag makes patterns case-insensitive, g finds all matches, and m enables multiline mode

Related Tools

Once you're comfortable with regex, explore these related tools:

Test Your Regex Patterns

Write, test, and debug regular expressions in real time with instant match highlighting.

Open Regex Tester →