Skip to content
uencoder

Regular Expression

A regular expression is a formal language for describing string patterns, defining matching rules with metacharacters and quantifiers; it is widely used for search, validation, and extraction.

Purpose

Provides a compact yet powerful string pattern-matching language, replacing hand-written parsing logic.

Mechanism

Composed of literal characters and metacharacters (. * + ? [] () {} \ | ^ $). Quantifiers control repetition (* zero or more, + one or more, ? zero or one, {n,m} a specified range). Grouping () captures, | alternates, [] is a character class, and \d \w \s are predefined classes. Engines match via NFA/DFA backtracking or automata.

Use cases

  • Form input validation (emails, phone numbers)
  • Log pattern extraction
  • Find and replace (IDEs, sed)
  • Lexer generation

Examples

  • Input
    /\d{3}-\d{4}/
    Output
    Matches 123-4567

    3 digits-4 digits

Common errors

The greedy quantifier .* matches too much
Use the non-greedy .*? or a precise character class

Related terms

Related tools

References