Master Python Regex: Find, Replace, and Split Strings with Real Code Examples
This article introduces regular expressions in Python, explains common regex symbols, and demonstrates how to use the re module's findall, sub, and split functions for string querying, replacement, and splitting, complemented by practical code examples and detailed explanations.
Regular expressions discover patterns in strings and express them with abstract symbols; for example, identifying the rule behind a numeric sequence allows calculation of subsequent values.
Common Regex Symbols
Familiarity with the most frequently used regex symbols is essential for effective string processing.
String Matching Query
The re.findall function iterates over a target string, returns all matching substrings as a list, and accepts three parameters: pattern (the regex), string (the input), and optional flags such as re.I (case‑insensitive), re.M (multi‑line), re.S (dot matches newline), and re.X (verbose mode).
String Replacement
The re.sub function replaces matches of a regex with a new value, similar to str.replace. Its parameters are pattern, repl (replacement string), string, optional count (max replacements, default all), and flags.
String Splitting
The re.split function divides a string according to a regex pattern, analogous to str.split. It takes pattern, string, optional maxsplit (maximum splits, default all), and flags.
Practical Examples
# Import the regex module
import re
# Example 1: extract weather states
string8 = "{ymd:'2018-01-01',tianqi:'晴',aqiInfo:'轻度污染'},{ymd:'2018-01-02',tianqi:'阴~小雨',aqiInfo:'优'}"
print(re.findall("tianqi:'(.*?)'", string8))
# Example 2: find words containing 'o' (case‑insensitive)
string9 = 'Together, we discovered that a free market only thrives when there are rules to ensure competition and fair play, Our celebration of initiative and enterprise'
print(re.findall('\w*o\w*', string9, flags=re.I))
# Example 3: remove punctuation, numbers, and letters from a Chinese text
string10 = '据悉,这次发运的4台蒸汽冷凝罐属于国际热核聚变实验堆(ITER)项目的核二级压力设备,先后完成了压力试验、真空试验、氦气检漏试验、千斤顶试验、吊耳载荷试验、叠装试验等验收试验。'
print(re.sub('[,。、a-zA-Z0-9()]', '', string10))
# Example 4: split a complex string and clean results
string11 = '2室2厅 | 101.62平 | 低区/7层 | 朝南
上海未来 - 浦东 - 金杨 - 2005年建'
split = re.split('[-|
]', string11)
split_strip = [i.strip() for i in split]
print(split_strip)The first example extracts only the weather condition values because the pattern uses parentheses to capture the group. The second example shows that findall returns a list of matching words, and adding parentheses would limit the result to the captured subgroup. The third example demonstrates using sub to delete punctuation, numbers, and letters, effectively cleaning the text. The fourth example splits a string by hyphens, pipes, or newlines, then removes surrounding whitespace from each element using a list comprehension and strip.
Conclusion
The tutorial covered the fundamentals of Python regular expressions, including how to query, replace, and split strings with the re module, and provided concrete code samples to reinforce understanding.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
