regular expression
This article is intended to introduce the RE package in Python.
The main funciton of RE module is
- sub:Replace matched items.
- match:Match from the beginning.
- search:Search the entire string.
- findall:Return a list of all matches.
- finditer:Return a list of all matches.
- split:Split a string based on a regex.
- compile:Complie a regex for effciency.
- escape:Escape special characters.
sub
Replace parts of the string that match the pattern wiht repl.
re.sub(pattern, repl, string, count=0, flags=0)
Demo
import re
result = re.sub(r'\d+', 'X', 'There are 6 straberries and 12 grapes')
print("Result:", result)
output:
Result: There are X straberries and X grapes.
match
Match the regular expression pattern from the beginning of the string.
re.match(pattern, string, flags=0)
Demo
import re
match = re.match(r'^Hello', 'Hello, world!')
if match:
print("Match found:", match.group())
else:
print("No match")
output:
Match found: Hello
search
Search the entire string for the first match of the regular expression.
re.search(pattern, string, flags=0)
Demo
import re
search = re.search(r'world', 'Hello, world!')
if search:
print("Match found:", search.group())
else:
print("No match")
output:
Match found: world
findall.
Return all non-overlapping matches of the pattern in the string as a list of strings.
matches = re.findall(pattern, sting, flags=0)
Demo
matches = re.findall(r'\d+', 'There are 6 strawberries and 12 grapes.')
print("Matches:", matches)
output:
Matches: ['6', '12']
finditer
Retirm an iterator yielding Match objects for all non-overlapping matches of the pattern in the string.
re.finditer(pattern, string, flags=0)
Demo
for match in re.finditer(r'\d+', 'There are 6 strawberries and 12 grapes.'):
print("Match:", match.group(), "at position:", match.span())
output:
Match: 6 at position: (10, 11)
Match: 12 at position: (29, 31)
split
Split the string by occurrences of the pattern.
re.split(pattern, string, maxsplit=0, flags=0)
Demo
result = re.split(r'\s+', 'Hello world this is Python written by Jellyfish')
print("Result:", result)
output:
Result: ['Hello', 'world', 'this', 'is', 'Python', 'written', 'by', 'Jellyfish']
compile
Compile a regular expression pattern into a Pattern object for more efficient use.
re.compile(pattern, flags=0)
Demo
pattern = re.compile(r'\d+')
matches = pattern.findall('There are 6 strawberries and 12 grapes.')
print("Matches:", matches)
output:
Matches: ['6', '12']
escape
Escape special characters in the string.
re.escape(string)
Demo
escaped = re.escape('Hello [world]!')
print("Escaped:", escaped)
output:
Escaped: Hello\ \[world\]!
浙公网安备 33010602011771号