Notes

Regular Expression Cleanup

Real text arrives dirty: tracking URLs, boilerplate footers, stray HTML entities, phone numbers, runs of exclamation marks, invisible control characters. Regular expression cleanup is the practice of fixing all of that with patterns — compact rules that match a shape of string rather than a specific word, and delete or rewrite every instance of it.

Three things the patterns do

  • Delete what carries no meaning: markup leftovers, zero-width characters, runs of whitespace collapsed to a single space.
  • Replace, when the presence of a thing is signal but its identity is not: a link becomes <URL>, a figure becomes <NUM>, an address becomes <EMAIL>. The model learns that a link was there without memorising which one.
  • Normalise a shape: curly quotes to straight ones, "!!!!" down to "!", "soooo" down to two letters, so the variants land on one token.

Where it bites
A greedy pattern such as <.*> swallows an entire line where <[^>]*> would have taken one tag — and neither should be used on nested markup, which wants a real parser. Nested quantifiers can backtrack catastrophically and hang a pipeline on one long line. Order matters as much as the patterns: strip URLs before punctuation removal, or the URL is already in pieces. Worst of all, a pattern is silent. It never reports what it matched, so a rule that is slightly too broad can remove a tenth of the corpus while the run reports success.

Why it matters
This is the first thing that touches raw text, so it bounds what every later step — tokenizer, vectorizer, model — can possibly see. Keep the patterns few, test each on a sample, and log how many substitutions each one made.

Regular expression cleanup uses pattern matching to strip or normalise non-linguistic noise in raw text — URLs, email addresses, markup remnants, control characters, runs of whitespace or repeated punctuation. Noise whose presence is itself signal is replaced with a placeholder such as <URL> rather than deleted. Being the first step over raw text, it bounds what every later stage can see, and an over-broad pattern removes real content while the pipeline still reports success.

Copy a recipe off a web page and you get the recipe plus a cookie banner, three share buttons and a tracking link. Clearing that out by hand is tedious, so people write patterns instead — short rules of the form "anything starting with http up to the next space is a link, so remove it". Regular expression cleanup is tidying text with a handful of those rules before any software reads it. It is fast, and it is blunt. A rule that is slightly too greedy deletes real sentences along with the junk, and nothing warns you, because the computer did exactly what it was told to do.