Module lessons (4/4)
re module: regex in Python
re.search / re.match / re.findall / re.sub, flags, raw strings r"...", and named groups.
What you'll learn in this lesson
- Writing safe patterns in Python using raw string
r"..."to avoid double backslashes. - Choose the right function from
re.search,re.match,re.fullmatch,re.findall,re.finditerandre.sub. - Extract structured substrings with positional capturing groups and named groups
(?P<name>...). - Prefill repeating expressions with
re.compile()and apply flags likere.IGNORECASEandre.MULTILINE.
Overview
The re module brings the power of regular expressions directly to Python. The number one rule is to write the patterns as raw string (r"\d+"): in a normal string, in fact, Python interprets some escapes before the regex even sees them (\b would become ASCII backspace, \n would become newline!), while r"..." delivers every character intact…
Always choose the narrowest operation for your purpose: avoid re.findall on giant texts when you only need the first occurrence (re.search) or a lazy stream (re.finditer).
Sections covered in this lesson
- 11. Raw string,
re.searchand walrus operator - 22.
re.findall, capture groups andre.finditer - 33. Named groups
(?P<name>...)and substitutions with function - 44.
re.compileand main flags - 5Check for understanding
- 6Test yourself
Unlock "re module: regex in Python" and the full Python Course curriculum
re.search / re.match / re.findall / re.sub, flags, raw strings r"...", and named groups.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Python Course plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.