Python中的正则表达式 – 第 2 组(搜索、匹配和查找全部)
Python中的正则表达式与示例|设置 1
模块re为Python中的正则表达式提供支持。以下是该模块中的主要方法。
搜索模式的出现
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 re.match().
import re
# a sample function that uses regular expressions
# to find month and day of a date.
def findMonthAndDate(string):
regex = r"([a-zA-Z]+) (\d+)"
match = re.match(regex, string)
if match == None:
print ("Not a valid date")
return
print ("Given Data: %s" % (match.group()))
print ("Month: %s" % (match.group(1)))
print ("Day: %s" % (match.group(2)))
# Driver Code
findMonthAndDate("Jun 24")
print("")
findMonthAndDate("I was born on June 24")
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)
# This example is contributed by Ayush Saluja.
输出 :
Match at index 14, 21
Full match: June 24
Month: June
Day: 24
将模式与文本匹配
re.match() :此函数尝试将模式匹配到整个字符串。 re.match函数在成功时返回匹配对象,在失败时返回 None。
re.match(pattern, string, flags=0)
pattern : Regular expression to be matched.
string : String where pattern is searched
flags : We can specify different flags
using bitwise OR (|).
Python3
# A Python program to demonstrate working
# of re.match().
import re
# a sample function that uses regular expressions
# to find month and day of a date.
def findMonthAndDate(string):
regex = r"([a-zA-Z]+) (\d+)"
match = re.match(regex, string)
if match == None:
print ("Not a valid date")
return
print ("Given Data: %s" % (match.group()))
print ("Month: %s" % (match.group(1)))
print ("Day: %s" % (match.group(2)))
# Driver Code
findMonthAndDate("Jun 24")
print("")
findMonthAndDate("I was born on June 24")
输出:
Given Data: Jun 24
Month: Jun
Day: 24
Not a valid date
查找所有出现的模式
re.findall() :返回字符串字符串。该字符串从左到右扫描,并按找到的顺序返回匹配项(来源: Python Docs)。
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)
# This example is contributed by Ayush Saluja.
输出 :
['123456789', '987654321']
正则表达式是一个广阔的话题。这是一个完整的图书馆。正则表达式可以做很多事情。您可以匹配、搜索、替换、提取大量数据。例如,下面的小代码非常强大,它可以从文本中提取电子邮件地址。所以我们可以用easy在Python中制作我们自己的Web Crawlers和scrappers。看下面的正则表达式。
# extract all email addresses and add them into the resulting set
new_emails = set(re.findall(r"[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+",
text, re.I))
我们很快将讨论更多关于正则表达式的方法。