Hasty Briefsbeta

Bilingual

The true power of regular expressions (2012)

4 hours ago
  • Regular expressions in programming are far more powerful than the formal 'regular languages' theory suggests, capable of matching context-free and even some context-sensitive languages.
  • The Chomsky hierarchy categorizes formal languages, and modern regex implementations like PCRE can handle languages beyond regular ones, including those with recursive patterns.
  • Using recursive subpatterns (e.g., (?1)) allows regex to match context-free languages like {a^n b^n}.
  • Grammar definitions can be directly translated into regex using DEFINE and named subpatterns, as shown with an RFC 5322 email regex.
  • Regex supports conversion of left-recursive grammars to right-recursive, maintaining expressive power.
  • Context-sensitive languages can be partially matched using lookaheads and backreferences, though limitations like fixed-width lookbehinds exist.
  • Backreferences make regex matching NP-complete, enabling regex to solve NP problems like 3-CNF SAT.
  • Despite regex's power, using it for complex tasks like HTML parsing is often impractical; DOM libraries are recommended for generic processing.
  • For well-defined, specific patterns, regex can be a simple and effective solution.

Related

Loading…