What is a regular expression (regex) and how do you write one?
Regular expressions are one of the most transferable skills in software development - learn regex once and you can use the same patterns in JavaScript, Python, Go, Ruby, SQL, terminal tools like grep and sed, and code editors like VS Code. The syntax differs slightly across implementations (PCRE vs. ECMAScript vs. RE2), but the core concepts are universal.
The building blocks of regex are: literals (the letter 'a' matches the character a), character classes ([aeiou] matches any vowel, [0-9] matches any digit), the dot (. matches any character except a newline), quantifiers (* means zero or more, + means one or more, ? means zero or one, {n} means exactly n times), anchors (^ is start of string, $ is end of string), and groups with parentheses for capture.
A practical example: validating an email address. The pattern ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$ checks for one or more valid characters before the @, a domain part after it, and a TLD of at least two letters. Real-world email validation regex can get far more complex, but this covers 99% of cases.
Common use cases developers reach for regex daily include: stripping HTML tags from a string, extracting all URLs from a document, validating that a password contains at least one uppercase letter and one number, parsing log files for specific error patterns, and replacing multiple spaces with a single space.
The best way to learn regex is with an interactive tester that highlights matches in real time as you type the pattern. This removes the trial-and-error cycle of writing a pattern, running code, seeing it fail, and repeating.