Fundamentals 7 min read

Using Regular Expressions in Python: Raw Strings, match, search, findall, and Grouping

This article explains how Python's re module works, covering raw string literals, the differences between match, search, and findall methods, how to retrieve match positions, and how to use numbered and named groups for extracting sub‑patterns from text.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Using Regular Expressions in Python: Raw Strings, match, search, findall, and Grouping

Regular expressions are not unique to Python, but the language has some subtle differences in their usage. The re module provides methods for searching and extracting patterns from strings.

Raw string literals are created by prefixing a normal string with r , preventing the Python interpreter from processing back‑slashes as escape characters, which simplifies writing regular expressions.

Using re.match – matches only at the beginning of the string. Example: import re then match = re.match(r'dog', 'dog cat dog') returns a match object whose group() method yields 'dog' . If the pattern does not appear at the start, no match is found.

Using re.search – scans the entire string and returns the first occurrence of the pattern. It can find a match anywhere, e.g., searching for 'cat' in 'dog cat dog' succeeds, while searching for 'dog' returns only the first 'dog' .

Using re.findall – returns a list of all non‑overlapping matches of the pattern. This is useful when you only need the matched substrings without the full match object.

Match object start and end – the match object provides start() and end() methods to locate the position of the matched substring within the original text.

Numbered groups – parentheses create capture groups that can be accessed with match.group(1) , match.group(2) , etc. Example:

contactInfo = 'Doe, John: 555-1212' match = re.search(r'(\w+), (\w+): (\S+)', contactInfo)

The groups correspond to last name, first name, and phone number respectively.

Named groups – groups can be given explicit names using the (?P<name>...) syntax, allowing access via match.group('name') . Example:

match = re.search(r'(?P<last>\w+), (?P<first>\w+): (?P<phone>\S+)', contactInfo)

Named groups improve readability, especially with many groups, though they are not supported by findall .

The article concludes that understanding raw strings, the basic re methods, and grouping techniques provides a solid foundation for using regular expressions in Python, and recommends the official Python documentation for further reading.

pythonmatchregexSearchre moduleRaw StringsGroupingfindall
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.