← Back to Blog

Regex Basics: A Complete Beginner's Guide

What is a Regular Expression?

A regular expression (regex) is a sequence of characters that defines a search pattern. Regex is used for string matching, validation, and text manipulation across virtually all programming languages.

Basic Literal Matching

The simplest regex is just literal text. The pattern hello matches the string "hello" exactly.

``

Pattern: hello

Text: Say hello to regex

Match: "hello" at position 4

`

Metacharacters

Metacharacters have special meanings in regex:

The Dot (.)

Matches any single character except newline:

`

Pattern: h.t

Matches: "hat", "hot", "hit", "h@t"

`

Character Classes []

Match any character within the brackets:

`

Pattern: [aeiou]

Matches: any vowel

Pattern: [0-9]

Matches: any digit

Pattern: [a-zA-Z]

Matches: any letter

`

Negated Classes [^]

Match anything NOT in the brackets:

`

Pattern: [^0-9]

Matches: any non-digit character

`

Quantifiers

Control how many times a pattern matches:

Common Quantifiers

  • * - Zero or more times
  • + - One or more times
  • ? - Zero or one time
  • {n} - Exactly n times
  • {n,} - n or more times
  • {n,m} - Between n and m times

`

Pattern: a+

Matches: "a", "aa", "aaa", etc.

Pattern: \d{3}

Matches: exactly 3 digits like "123"

`

Anchors

Match positions rather than characters:

  • ^ - Start of string/line
  • $ - End of string/line
  • \b - Word boundary

`

Pattern: ^Hello

Matches: "Hello" only at the start

Pattern: world$

Matches: "world" only at the end

`

Shorthand Character Classes

  • \d - Any digit [0-9]
  • \D - Any non-digit
  • \w - Any word character [a-zA-Z0-9_]
  • \W - Any non-word character
  • \s - Any whitespace
  • \S - Any non-whitespace

Your First Regex

Let's validate an email (simplified):

`

Pattern: \w+@\w+\.\w+

Breakdown:

  • \w+ : One or more word characters (username)
  • @ : Literal @ symbol
  • \w+ : One or more word characters (domain)
  • \. : Literal dot (escaped)
  • \w+ : One or more word characters (TLD)
``

Practice Tips

  • Start simple and build up complexity
  • Test with multiple inputs
  • Use a regex tester like RegexSpark
  • Comment complex patterns for future reference
  • Try RegexSpark

    Test and debug your regular expressions in real-time with our free online tool.

    Test Regex Now