


We use cookies to improve your experience
We use essential cookies to make our site work. With your consent, we may also use non-essential cookies to improve user experience.
Definition
Regex (Regular Expression) is a sequence of characters that defines a search pattern for matching text. Regex is used for text search, validation (email, phone numbers), find-and-replace, and data extraction across virtually all programming languages and text editors.
Regular expressions provide a concise language for describing text patterns. The pattern /\d{3}-\d{4}/ matches phone numbers like "555-1234". The pattern /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ validates email addresses. Regex combines literal characters with special metacharacters: . (any character), * (zero or more), + (one or more), ? (optional), [] (character class), () (groups), ^ (start), $ (end).
Regex is supported by all major programming languages (JavaScript, Python, Java, Go, Rust, etc.), text editors (VS Code, Sublime, Vim), command-line tools (grep, sed, awk), and databases (PostgreSQL, MySQL). Each implementation has slight syntax variations, known as "flavors" — PCRE, JavaScript, POSIX, and others.
While powerful, regex has well-known limitations. Complex patterns become difficult to read and maintain ("write-only code"). Regex is not suitable for parsing nested structures like HTML or JSON — a common mistake that leads to fragile solutions. Performance can also be a concern: poorly constructed patterns with excessive backtracking can cause catastrophic performance (ReDoS vulnerabilities). For complex text processing, combining regex with procedural code is usually better than trying to do everything in a single pattern.