Fundamentals 10 min read

Master Python Regex: Raw Strings, match, search, findall, and Grouping

This article introduces Python's regular expression module, explaining raw string literals, the differences between match, search, and findall methods, and how to use numbered and named groups to extract specific parts of matched text, complete with practical code examples.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python Regex: Raw Strings, match, search, findall, and Grouping

Regular expressions are not unique to Python, but Python's re module has some nuances that are important when using them in practice.

We start by importing the module:

import re

1. Raw string literals

Python uses backslashes for escape sequences (e.g., \n for a newline). Since re also uses backslashes to escape special regex characters, you often need to escape the backslash itself. To avoid this confusion, prefix a string with r to create a raw string, which tells the Python compiler not to interpret backslashes.

Example:

string = 'This is a
normal string'
rawString = r'This is a
raw string'

Printing string shows a newline, while printing rawString shows the literal \n.

2. Searching with re.match – match at the start

re.match(r'dog', 'dog cat dog')

returns a match object because the pattern appears at the beginning of the string. Calling match.group(0) yields 'dog'. If the pattern does not appear at the start, re.match returns None.

3. Searching with re.search – match anywhere

re.search(r'cat', 'dog cat dog')

finds 'cat' even though it is not at the start. re.search stops after the first match, so searching for 'dog' returns only the first occurrence.

4. Finding all matches with re.findall

re.findall(r'dog', 'dog cat dog')

returns a list ['dog', 'dog']. When the pattern contains groups, findall returns a list of tuples with the captured groups.

5. Using match.start() and match.end()

The match object provides the start and end indices of the matched substring, e.g., match.start() returns 0 and match.end() returns 3 for the word 'dog' at the beginning of the string.

6. Numbered groups

Parentheses create capture groups. For example, with contactInfo = 'Doe, John: 555-1212': match = re.search(r'(\w+), (\w+): (\S+)', contactInfo) Then match.group(1) yields 'Doe', match.group(2) yields 'John', and match.group(3) yields '555-1212'. Group 0 returns the entire match.

7. Named groups

Named groups improve readability:

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

Now match.group('last') returns 'Doe', match.group('first') returns 'John', and match.group('phone') returns '555-1212'.

Although findall does not return a match object, it can still capture groups and returns a list of tuples.

In summary, this article covered the basics of using Python regular expressions: raw strings, the match, search, and findall functions, and both numbered and named grouping techniques.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonmatchregexSearchraw-stringsgroupingfindallre-module
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

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.