在Python的正则表达式中,有一个参数为re.S。它表示将“.”这个匹配符的作用扩展到整个字符串,包括“\n”。
在正则表达式中, ‘.’的作用是表示匹配字符串中除了‘\n’的任意字符,如果不加 re.S ,每当遇到换行符,就会停止匹配。而使用re.S参数以后,正则表达式会将这个字符串作为一个整体,将“\n”当做一个普通的字符加入到这个字符串中,在整体中进行匹配。
以下是代码的案例:
01
02
03
04
05
06
07
08
09
10
11
12
13
|
import re str = ‘‘‘ This is Heima Itcast ‘‘‘ a = re.findall ( ‘This is ( . * ? ) Itcast‘ , str ) b = re.findall ( ‘This is ( . * ? ) Itcast‘ , str , re.S ) print ( ‘a is ‘ , a ) print ( ‘b is ‘ , b ) |
运行结果
a is []
b is [‘ \n Heima\n ‘]
更多技术资讯可关注:gzitcast
原文:https://www.cnblogs.com/heimaguangzhou/p/11640684.html