Back to Blog
regex 2025-01-14

Regular Expressions Basics for Beginners

Learn the fundamentals of regular expressions with practical examples.

Regular expressions (regex) are powerful patterns for matching text.

Basic Patterns

PatternMatches

|---------|---------|

.Any single character \dAny digit (0-9) \wWord character (a-z, A-Z, 0-9, _) \sWhitespace ^Start of string $End of string

Quantifiers

QuantifierMeaning

|------------|---------|

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

Character Classes

[abc]    # a, b, or c

[^abc] # Not a, b, or c

[a-z] # a through z

[A-Za-z] # Any letter

Common Patterns

Email validation:

^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$

Phone number:

^\d{3}-\d{3,4}-\d{4}$

URL:

^https?://[\w.-]+(/[\w./-]*)?$

Groups and Capturing

(abc)     # Capturing group

(?:abc) # Non-capturing group

\1 # Backreference to group 1

Flags

  • g: Global (find all matches)
  • i: Case insensitive
  • m: Multiline mode

Use our Regex Tester to practice and validate your patterns.