defmatch(pattern, string, flags=0):"""Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found."""return _compile(pattern, flags).match(string)
deffullmatch(pattern, string, flags=0):"""Try to apply the pattern to all of the string, returning
a match object, or None if no match was found."""return _compile(pattern, flags).fullmatch(string)
defsearch(pattern, string, flags=0):"""Scan through string looking for a match to the pattern, returning
a match object, or None if no match was found."""return _compile(pattern, flags).search(string)
deffindall(pattern, string, flags=0):"""Return a list of all non-overlapping matches in the string.
If one or more capturing groups are present in the pattern, return
a list of groups; this will be a list of tuples if the pattern
has more than one group.
Empty matches are included in the result."""return _compile(pattern, flags).findall(string)
deffinditer(pattern, string, flags=0):"""Return an iterator over all non-overlapping matches in the
string. For each match, the iterator returns a match object.
Empty matches are included in the result."""return _compile(pattern, flags).finditer(string)
def split(pattern, string, maxsplit=0, flags=0):
"""Split the source string by the occurrences of the pattern,
returning a list containing the resulting substrings. If
capturing parentheses are used in pattern, then the text of all
groups in the pattern are also returned as part of the resulting
list. If maxsplit is nonzero, at most maxsplit splits occur,
and the remainder of the string is returned as the final element
of the list."""
return _compile(pattern, flags).split(string, maxsplit)
def sub(pattern, repl, string, count=0, flags=0):
"""Return the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in string by the
replacement repl. repl can be either a string or a callable;
if a string, backslash escapes in it are processed. If it is
a callable, it's passed the match object and must return
a replacement string to be used."""
return _compile(pattern, flags).sub(repl, string, count)
# 導入正則表達式模塊import re
# 定義測試文本字符串,我們后續在這段文本中查詢數據
msg1 = """Python is an easy to learn, powerful programming language.
It has efficient high-level data structures and a simple but effective approach to object-oriented programming.
Python’s elegant syntax and dynamic typing, together with its interpreted nature,
make it an ideal language for scripting and rapid application development in many areas on most platforms.
"""
msg2 = "hello"
msg3 = "hello%"# 定義正則表達式,匹配字符串開頭是否為python
regStart = r"efficient"# 從字符串開始位置匹配,是否包含符合正則表達式的內容,返回匹配到的字符串的Match對象
print(re.match(regStart, msg1))
# 掃描整個字符串,是否包含符合正則表達式的內容,返回匹配到的第一個字符串的Match對象
print(re.search(regStart, msg1))
# 掃描整個字符串,是否包含符合正則表達式的內容,返回匹配到的所有字符串列表
print(re.findall(regStart, msg1))
# 掃描整個字符串,是否包含符合正則表達式的內容,返回匹配到的字符串的迭代對象for r in re.finditer(regStart, msg1):
print("->"+ r.group())
# 掃描整個字符串,是否包含在正則表達式匹配的內容中,是則返回整個字符串,否則返回None
print(re.fullmatch(r"\w*", msg2))
print(re.fullmatch(r"\w*", msg3))