Markdown

Regex

Regex is the Acronym for Regular Expression

A sequence of characters that defines a search pattern, Regex (regular expression) is primarily used for string matching, data validation, and search-and-replace operations. It allows you to find complex patterns, like email addresses, phone numbers, or specific code structures, within a body of text using a single line of logic.

Key Specifications

Regex relies on several key components to define its search logic:

  • Engine: The software component (e.g., PHP, JS, Python re) that interprets the regex.
  • Literal Characters: Characters that match themselves (e.g., abc matches "abc").
  • Metacharacters: Special symbols (like *, +, ?, .) that define the logic of the search.
  • Delimiters: Characters (usually /) used to wrap the expression, e.g., /pattern/flags.

Regexp Syntax "Cheat Sheet"

SymbolNameDescriptionExample
.WildcardMatches any single character except a newline.h.t matches "hat", "hot"
^Anchor (Start)Matches the beginning of a string.^Hello
$Anchor (End)Matches the end of a string.world$
*QuantifierMatches 0 or more of the preceding element.ab* matches "a", "ab", "abbb"
+QuantifierMatches 1 or more of the preceding element.ab+ matches "ab", "abbb"
dDigitMatches any single numerical digit (0-9).dd matches "42"
[a-z]Character SetMatches any single character within the brackets.[A-C] matches "A", "B", or "C"
(…)Capture GroupGroups multiple tokens together for extraction.(abc)+

Practical Example: Email Validation

A simplified regexp to find an email address might look like this: /b[w.-]+@[w.-]+.[a-zA-Z]{2,6}b/g

  • [w.-]+: Looks for one or more letters, numbers, dots, or dashes.
  • @: Looks for the literal "@" symbol.
  • .: Looks for a literal period (the backslash escapes it so it's not a wildcard).
  • {2,6}: Ensures the top-level domain (like .com or .org) is between 2 and 6 characters long.

Common Pitfalls

Regex is incredibly powerful but notoriously difficult to read once it reaches a certain complexity. This has led to the famous industry joke: "Some people, when confronted with a problem, think 'I know, I'll use regular expressions.' Now they have two problems." , Jamie Zawinski

  • Greediness: Quantifiers like * are greedy by default, meaning they match as much text as possible. This can lead to matching more than you intended.
  • Readability: Complex regex one-liners can be nearly impossible for teammates (or your future self) to debug.

Test a Pattern in the Browser

Readability is the hard part of a long pattern. The Regex Tester colors a JavaScript pattern and writes each piece in plain English, under the lines you are testing. Paste one phrase per line. Each line is marked pass or fail. The test runs in the browser, so a pattern that passes there can still fail in PHP, nginx, or Google Analytics (GA).

Articles Tagged Regex

View Additional Articles Tagged Regex