Python正则表达式:re.search() VS re.findall()
先决条件:带有示例的正则表达式 | Python
正则表达式(有时称为 Rational 表达式)是定义搜索模式的字符序列,主要用于与字符串的模式匹配或字符串匹配,即“查找和替换”类操作。正则表达式是一种将模式与字符序列匹配的通用方法。
模块正则表达式 (RE)指定一组与其匹配的字符串(模式)。为了理解 RE 的类比, MetaCharacters
是有用的、重要的,并且将用于模块re
的功能中。
共有 14 个元字符,将在它们遵循函数时进行讨论:
\ Used to drop the special meaning of character
following it (discussed below)
[] Represent a character class
^ Matches the beginning
$ Matches the end
. Matches any character except newline
? Matches zero or one occurrence.
| Means OR (Matches with any of the characters
separated by it.
* Any number of occurrences (including 0 occurrences)
+ One or more occurrences
{} Indicate number of occurrences of a preceding RE
to match.
() Enclose a group of REs
研究()
re.search()
方法要么返回 None (如果模式不匹配),要么返回一个re.MatchObject
,其中包含有关字符串匹配部分的信息。此方法在第一次匹配后停止,因此它最适合测试正则表达式而不是提取数据。
例子:
Python3
# A Python program to demonstrate working of re.match().
import re
# Lets use a regular expression to match a date string
# in the form of Month name followed by day number
regex = r"([a-zA-Z]+) (\d+)"
match = re.search(regex, "I was born on June 24")
if match != None:
# We reach here when the expression "([a-zA-Z]+) (\d+)"
# matches the date string.
# This will print [14, 21), since it matches at index 14
# and ends at 21.
print("Match at index % s, % s" % (match.start(), match.end()))
# We us group() method to get all the matches and
# captured groups. The groups contain the matched values.
# In particular:
# match.group(0) always returns the fully matched string
# match.group(1) match.group(2), ... return the capture
# groups in order from left to right in the input string
# match.group() is equivalent to match.group(0)
# So this will print "June 24"
print("Full match: % s" % (match.group(0)))
# So this will print "June"
print("Month: % s" % (match.group(1)))
# So this will print "24"
print("Day: % s" % (match.group(2)))
else:
print("The regex pattern does not match.")
Python3
# A Python program to demonstrate working of
# findall()
import re
# A sample text string where regular expression
# is searched.
string = """Hello my Number is 123456789 and
my friend's number is 987654321"""
# A sample regular expression to find digits.
regex = '\d+'
match = re.findall(regex, string)
print(match)
输出:
Match at index 14, 21
Full match: June 24
Month: June
Day: 24
re.findall()
返回字符串字符串。从左到右扫描字符串,并按找到的顺序返回匹配项。
例子:
Python3
# A Python program to demonstrate working of
# findall()
import re
# A sample text string where regular expression
# is searched.
string = """Hello my Number is 123456789 and
my friend's number is 987654321"""
# A sample regular expression to find digits.
regex = '\d+'
match = re.findall(regex, string)
print(match)
输出:
['123456789', '987654321']