AS
@CertainPerformance
说,用
re.M
结束时的标志
findall
:
print(re.findall(my_regex,my_string,re.M))
演示:
>>> import re
>>> my_regex = r'^Credits$'
>>> my_string = "based upon extrinsic circumstances, as discussed in Serrano v. Priest, 20 Cal.3d 25, 49.\n\nCredits\n(Added by Stats.1977, c. 1197, p. 3979, 1. Amended by Stats.1993, c. 645 (S.B.764), 2.)"
>>> print(re.findall(my_regex,my_string,re.M))
['Credits']
或与一起使用
r'(?m)^Credits$'
:
>>> import re
>>> my_regex = r'(?m)^Credits$'
>>> my_string = "based upon extrinsic circumstances, as discussed in Serrano v. Priest, 20 Cal.3d 25, 49.\n\nCredits\n(Added by Stats.1977, c. 1197, p. 3979, 1. Amended by Stats.1993, c. 645 (S.B.764), 2.)"
>>> print(re.findall(my_regex,my_string,re.M))
['Credits']