Cleaning Text with Python and Regex
Clean and normalise messy text for AI pipelines with string methods, Unicode care and readable regex.
A taste of a lesson
My regex (.*) grabs from the first bold tag to the very last one. Why?
Because .* is greedy: it matches as much as it can, so it runs to the last on the line. Two fixes. Make it lazy with (.*?), which stops at the first closing tag, or tighten the class with ([^<]*), which cannot cross into another tag at all. For real HTML, though, an HTML parser is safer than regex, since tags can nest or contain attributes. Try both patterns on 'a one b two'. What does each return?
Written by the teacher as an example. In your lesson the tutor answers your own questions, and like any AI it can be wrong.
What you will be able to do
- Clean whitespace, HTML and boilerplate from real text with string methods
- Normalise Unicode so visually identical text matches
- Write, test and explain small regular expressions
- Decide how much cleaning suits embeddings, prompts or classic NLP
- Redact common personal data patterns while knowing their limits
Lesson plan
- 1 Decide what clean means for your use Match the amount of cleaning to how the text will be used. Start
- 2 String methods and whitespace Handle most everyday cleaning with built in string methods. Start
- 3 Unicode and HTML Normalise characters and extract text from HTML safely. Start
- 4 Regular expressions step by step Build small regex patterns and test them against good and bad examples. Start
- 5 Extract and replace with regex Use findall, search and sub to pull out and rewrite patterns. Start
- 6 Redaction and its limits Replace common personal data patterns and check what slipped through. Start
Try asking
About this tutor
For beginners preparing text for embeddings, classification or prompts: scraped pages, support tickets, transcripts, product descriptions. You learn string methods, Unicode normalisation, whitespace and HTML clean up, and enough regular expressions to find, extract and replace patterns without writing unreadable code. The tutor is clear about what not to do: modern language models handle case and punctuation well, so aggressive cleaning from older NLP tutorials often removes useful signal. You also practise simple redaction of emails and phone numbers, with honest notes on what regex will miss.
Reviews
4.7
3 ratingsSample
- Chiara F.Sample
I was lowercasing and removing stop words before embedding, because an old tutorial said so. The first lesson explained why that hurts. Regex section was patient and practical.
- Femi J.Sample
Good exercises on greedy versus lazy matching. The redaction lesson was honest that regex misses things, which I appreciated, though I wanted more on names.
- Lucy N.Sample
Unicode normalisation explained why two identical looking product names never matched. Small fix, big relief.
About the teacher
Python teacher for people starting AI work from zero, patient with setup, files and first scripts
9 tutors 377 lessons taught Sample
I teach Python to people who want to build with AI but have never written much code, or who tried once and got stuck on setup. My background is in teaching adult evening coding classes and later writing data scripts for small research teams, so I know where beginners lose an afternoon: the wrong interpreter, a confusing traceback, a file...
See Efua's profile and tutorsMore like this
Other tutors on the same or nearby topics.