Regex is genuinely good at most of this and completely useless at the rest, and the boundary is sharp enough to be worth knowing before you start.
What has a shape
These are reliable, cheap and deterministic. Match them first: they are most of the volume and none of the difficulty.
- •Email addresses. Do not try to implement RFC 5322 — a pragmatic pattern catches what people actually paste, and the full grammar catches things nobody has ever typed.
- •Card numbers. Match 13–19 digits with optional separators, then run a Luhn check. Skipping Luhn is the classic mistake: half your matches will be order numbers and reference codes.
- •IBANs. Country code, two check digits, then a country-specific length. The mod-97 check is four lines and removes nearly every false positive.
- •Phone numbers. The hard part is not the pattern, it is deciding which formats you actually receive. A generic international pattern matches dates, invoice numbers and product codes.
- •National IDs. Each has its own shape and most have a checksum. Use it.
- •Postcodes and ZIPs. Easy, and worth doing because they are more identifying in combination than people assume.
- •API keys and connection strings. Prefixes are your friend: sk-, ghp-, AKIA, xoxb-. People paste these far more often than they admit.
The first thing regex cannot do
Names. A name is a word, and there is no pattern that distinguishes Marta Kowalczyk from Northgate Supplies, or from any capitalised phrase at the start of a sentence.
Two approaches work and they fail in opposite directions. Cue-based detection looks for a label in front of the value — Customer:, Patient name:, Dear, Regards — and is accurate on records, forms and letters, which is what people mostly paste. It finds nothing in prose. A named-entity model finds names in prose and brings latency, probability, and a decision about what to do when it cannot tell.
Regex finds the things with shapes. The people are the things without.
The second thing regex cannot do
Combinations. Every individual field can be unremarkable and the row still identifies somebody: a job title, a town, a start date and an employer is one person in most organisations. No pattern matches that, because nothing in it is a pattern.
This is why “we strip PII with regex” is a claim worth interrogating. It is usually true about the fields and false about the record.
Mistakes that survive review
- •Matching on the raw string only. Text arrives with zero-width characters, non-breaking spaces and full-width digits pasted from documents. Normalise first or your patterns miss the real inputs and catch the test ones.
- •Replacing shortest-first. Redact a phone number inside an address before the address itself and you leave half a match behind.
- •Forgetting the system prompt. People put customer context in there, and it gets sent on every single turn.
- •Redacting the request and logging the original. Your traces are a copy of everything you just carefully removed.
What to do about the gap
Accept it and design for it. Match the structured values with patterns, use cues where your inputs are records, and decide explicitly what happens when the name stage cannot answer — proceed or refuse, chosen per request rather than by accident.
And substitute rather than mask. A model handed [NAME] writes about [NAME]; handed a plausible different name it writes an ordinary sentence you can restore. That single decision determines whether the output is usable, and it is independent of how good your detection is.
If you would rather not maintain this, it is what our API does — including the cue-based name stage and the substitution, with the mapping returned to you and stored nowhere.
Try Secure AI free
Frontier AI chat — encrypted by default, with your identity stripped before any request reaches the provider.
Common questions
Can regex detect PII reliably?
For anything with a shape, yes: emails, card numbers, IBANs, phone numbers, national IDs, postcodes, API keys. Add the checksum where one exists — Luhn for cards, mod-97 for IBANs — or a large share of your matches will be reference numbers.
Why can't regex find names?
Because a name has no distinguishing shape. It is a capitalised word, and so are company names, product names, place names and the first word of every sentence. Detection needs either a cue in front of the value or a model that understands context.
Is masking PII enough before sending to an LLM?
It is enough for privacy and poor for usefulness. A model given placeholders writes about placeholders, and the output needs repair. Substituting plausible values keeps the text coherent and lets you restore the real ones in the response.
What normalisation should I do first?
Strip zero-width characters, convert non-breaking spaces, fold full-width digits to ASCII, and normalise Unicode. Text pasted out of PDFs and Word documents carries all of these, and patterns written against clean test data miss them.