Skip to content
CoreSource checked

Regular Expression Matching

Matches text against patterns with operators such as choice, repetition, and character classes.

Quick Facts

Compact metadata for implementation and discovery.

Category
Pattern Matching
Difficulty
Intermediate
Family
Automaton Matching
Tier
Core
Status
Verified
Review
Source Checked
Sources
2
Formulas
1
Relationships
3
Code example
Ready
Visualization
Ready
Aliases
Regex Matching

Overview

Regular Expression Matching compiles or interprets a pattern and checks whether text follows that pattern language.

Core Concepts

pattern comparisonstring sequencefinite-state automatonautomaton matchingsequence scanpattern statematch verification

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(n)

Average

O(nm)

Worst

O(2^n)

Space

O(m)

When to Use

  • Use Regular Expression Matching when you need to represent pattern states.
  • Use it for input validation with inputs like regular expression.
  • Use it when exact, fuzzy, or structured patterns must be found in sequences.
  • Use it when the Automaton Matching tradeoff is acceptable for the problem size.

When Not to Use

  • Avoid it when preprocessing cost is larger than a one-off simple scan.
  • Avoid it when O(2^n) worst-case behavior is too costly for the expected input size.
  • Avoid it when the required output is not match result.

What It Does

  • Represents pattern states
  • Consumes text according to regex operators

Use Cases

  • Input validation
  • Text extraction
  • Search tools

Inputs

  • Regular expression
  • Text

Outputs

  • Match result
  • Capture groups when supported

Key Formulas (1)

Automaton transition

nextstates = εclosure(move(currentstates, character))
next_states = epsilon_closure(move(current_states, character))

Advances all active pattern states for one input character.

Pseudocode

  1. 1.Parse or compile the regular expression
  2. 2.Create matching states
  3. 3.Feed text characters through the matcher
  4. 4.Track active states or backtracking choices
  5. 5.Return matches that reach accepting states

Canonical Example

Input
text = 'abracadabra', pattern = 'abra'
Output
matches at positions [0, 7]
Explanation
Regular Expression Matching: The algorithm scans the sequence and reports where the pattern appears.

Implementation Notes

  • Keep the input contract explicit: Regular expression; Text.
  • Use this step as the implementation spine: Parse or compile the regular expression.
  • Then preserve the next state change: Create matching states.
  • Decide whether indexes mean bytes, code units, characters, or tokens.
  • Test overlapping matches, empty patterns, repeated characters, and no-match inputs.

Common Pitfalls

  • Backtracking regex engines can have exponential cases unless patterns are constrained.
  • Indexing by bytes instead of characters can break text with multibyte characters.
  • Reporting a match before verifying collisions, boundaries, or full-pattern alignment.
  • Returning match result without handling empty, invalid, or boundary inputs.

Interactive Mechanism Walkthrough

Sequence Window & Automaton Alignment

Show text moving through pattern states and accepting matches.

Input:ABABABCABAB
Window:ABAB
Step 1 of 5: Parse or compile the regular expression
regex-states

Notes

Regular Expression Matching operates in O(nm) time and O(m) space within the Automaton Matching family. Key governing relation: next_states = epsilon_closure(move(current_states, character)). Often compared with Aho-Corasick and Naive String Search.

Sources (2)

What each reference supports for this entry.

Algorithms on Strings, Trees, and Sequences

Book

String algorithms reference covering Regular Expression Matching recurrences, automata/indexing structures, or exact/approximate matching role.

Thompson Regular Expression Search

Paper

Original reference for compiling regular expressions to automata and simulating matches over text.

Open source

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

Regular-expression matching can compile patterns to finite automata before scanning text.

Tags

Pattern MatchingStringsAutomaton