Intelligently generate and explain regular expressions, supporting various common pattern matching
Convert your text instructions into formulas or input a formula to have it explained.
Edit Excel online by chatting with AI
Convert your text instructions into SQL queries - powered by AI.
Generate Excel VBA (Visual Basic for Applications) code to automate tasks and create custom solutions within Microsoft Excel.
Upload your Excel file and generate beautiful charts with our AI-powered chart generator.
Convert your text into beautiful mind maps with our AI-powered mind map generator. Edit and customize your mind maps easily.
Use AI to intelligently generate and explain regular expressions, supporting various text pattern matching and data validation.
Regular expressions are powerful tools for text processing, widely used in Python data analysis, web scraping, log processing, and other fields. This tutorial will guide you through systematically mastering Python’s re module and demonstrate how to efficiently process text data through practical examples.
Regular expressions play an important role in data processing:
Studies show that professional developers can significantly improve work efficiency using regular expressions in text processing tasks, especially when dealing with complex text patterns.
import re
pattern = r"hello"
text = "hello world"
result = re.match(pattern, text)
if result:
print("Match successful:", result.group()) # Output: hello
text = "Python最新版本3.9发布了"
match = re.search(r'\d+.\d+', text)
if match:
print("Found version number:", match.group()) # Output: 3.9
contact_info = "邮箱: [email protected], 客服: [email protected]"
emails = re.findall(r'[\w\.-]+@[\w\.-]+', contact_info)
print(emails) # ['[email protected]', '[email protected]']
Character | Function Description | Practical Example |
---|---|---|
. | Matches any single character | a.c → “abc” |
\d | Matches a digit character | \d\d → “42” |
\w | Matches a word character | \w+ → “Var123” |
\s | Matches a whitespace character | a\sb → “a b” |
Quantifier | Matching Rule | Typical Usage |
---|---|---|
* | Zero or more occurrences | a*b → “b”, “aaaab” |
+ | One or more occurrences | a+b → “ab”, “aaaab” |
{n,m} | n to m occurrences | a{2,4}b → “aab”, “aaaab” |
log_entry = "2023-05-15 14:30:22 [ERROR] System crash"
match = re.match(r'(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}) \[(\w+)\]', log_entry)
if match:
date, time, level = match.groups()
print(f"Error occurred on {date} {time}, Level: {level}")
html_content = "<p>第一段</p><p>第二段</p>"
# Greedy mode
print(re.findall(r'<p>(.*)</p>', html_content))
# Non-greedy mode
print(re.findall(r'<p>(.*?)</p>', html_content))
# Extract Python followed by a digit
code_text = "Python3 Python2 Python"
print(re.findall(r'Python(?=\d)', code_text))
# Extract Python not followed by a digit
print(re.findall(r'Python(?!\d)', code_text))
contact_text = "办公室: 010-87654321, 手机: 13912345678"
phone_numbers = re.findall(r'\b\d{3}-\d{8}\b|\b1[3-9]\d{9}\b', contact_text)
print(phone_numbers) # ['010-87654321', '13912345678']
def check_password_strength(password):
"""Validate password contains uppercase and lowercase letters and digits, length 8-20 characters"""
pattern = r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[\w]{8,20}$'
return re.match(pattern, password) is not None
print(check_password_strength("Secure123")) # True
print(check_password_strength("weak")) # False
Improve Regular Expression Efficiency:
re.compile()
to precompile common
patterns
(?:...)
Prevent Typical Errors:
.
, *
,
+
, ?
need to be escaped
correctly
\u
for matching Unicode characters
^[w\.-]+@[\w\.-]+\.\w+$
https?://[^\s]+
[\u4e00-\u9fa5]
\d{4}-\d{2}-\d{2}