A regular expression is a tiny pattern language for finding and extracting text — email addresses, dates, anything with a shape. The safest way to build one is to test it against real examples as you go, so you can see exactly what it matches before you run it on anything important.
Build it against real text
Paste a chunk of your actual data and type the pattern. A live tester highlights every match as you type, so you immediately see false positives (things you did not mean to catch) and misses. This trial-and-error loop is far faster and safer than writing a pattern blind and running it against a whole file.
How to test one
- Open the regex tester.
- Paste sample text and type your pattern.
- Adjust flags (like
gfor all matches andifor case-insensitive) and watch the highlights update.
The patterns worth memorising
\d+— one or more digits.\w+— a word (letters, digits, underscore)..*?— anything, but as little as possible (non-greedy).^and$— the start and end of a line.
The most common regex bug is a greedy .* that grabs far more than you intended. Adding a ? to make it .*? (non-greedy) fixes a huge share of 'it matched too much' problems.