您的位置 首页 java

Python正则表达式的使用方法(上)

Python正则表达式的使用方法(上)

Python基础教程:正则表达式的使用方法!

正则表达式

本节我们看一下正则表达式的相关用法,正则表达式是处理 字符串 的强大的工具,它有自己特定的语法结构,有了它,实现字符串的检索、替换、匹配验证都不在话下。

当然对于爬虫来说,有了它,我们从HTML里面提取我们想要的信息就非常方便了。

python学习关注我们企鹅qun: 8393 83765 各类入门学习资料免费分享哦!

实例引入

说了这么多,可能我们对它到底是个什么还是比较模糊,下面我们就用几个实例来感受一下正则表达式的用法。

我们打开开源中国提供的正则表达式测试工具,打开之后我们可以输入待匹配的文本,然后选择常用的正则表达式,就可以从我们输入的文本中得出相应的匹配结果了。

例如我们在这里输入待匹配的文本如下:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family : Consolas, Menlo, Courier, monospace; font-size: 16px; white-space : pre-wrap; position: relative; line-height: 1.5; color: rgb (153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>Hello, my phone number is 010-86432100 and email is cqc@cuiqingcai.com, and my website is

</pre>

这段字符串中包含了一个电话号码和一个电子邮件,接下来我们就尝试用正则表达式提取出来。

我们在网页中选择匹配Email地址,就可以看到在下方出现了文本中的Email。如果我们选择了匹配网址URL,就可以看到在下方出现了文本中的URL。是不是非常神奇?

其实,在这里就是用了正则表达式匹配,也就是用了一定的规则将特定的文本提取出来。比如电子邮件它开头是一段字符串,然后是一个@符号,然后就是某个域名,这是有特定的组成格式的。另外对于URL,开头是协议类型,然后是冒号加双斜线,然后是域名加路径。

对于URL来说,我们就可以用下面的正则表达式匹配:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>[a-zA-z]+://[^s]*

</pre>

如果我们用这个正则表达式去匹配一个字符串,如果这个字符串中包含类似URL的文本,那就会被提取出来。

这个正则表达式看上去是乱糟糟的一团,其实不然,这里面都是有特定的语法规则的。比如a-z代表匹配任意的小写字母,s表示匹配任意的空白字符,*就代表匹配前面的字符任意多个,这一长串的正则表达式就是这么多匹配规则的组合,最后实现特定的匹配功能。

写好正则表达式后,我们就可以拿它去一个长字符串里匹配查找了,不论这个字符串里面有什么,只要符合我们写的规则,统统可以找出来。那么对于网页来说,如果我们想找出网页源代码里有多少URL,就可以用匹配URL的正则表达式去匹配,就可以得到源码中的URL了。

在上面我们说了几个匹配规则,那么正则表达式的规则到底有多少?那么在这里把常用的匹配规则总结一下:

模式描述

w匹配字母数字及下划线

W匹配非字母数字及下划线

s匹配任意空白字符,等价于 [ ].

S匹配任意非空字符

d匹配任意数字,等价于 [0-9]

D匹配任意非数字

A匹配字符串开始

Z匹配字符串结束,如果是存在换行,只匹配到换行前的结束字符串

z匹配字符串结束

G匹配最后匹配完成的位置

匹配一个换行符

匹配一个制表符

^匹配字符串的开头

$匹配字符串的末尾。

.匹配任意字符,除了换行符,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符。

[…]用来表示一组字符,单独列出:[amk] 匹配 ‘a’,’m’或’k’

[…]不在[]中的字符:[abc] 匹配除了a,b,c之外的字符。

*匹配0个或多个的表达式。

+匹配1个或多个的表达式。

?匹配0个或1个由前面的正则表达式定义的片段,非贪婪方式

{n}精确匹配n个前面表达式。

{n, m}匹配 n 到 m 次由前面的正则表达式定义的片段,贪婪方式

a|b匹配a或b

( )匹配括号内的表达式,也表示一个组

可能完了之后就有点晕晕的了把,不用担心,下面我们会详细讲解下一些常见的规则的用法。怎么用它来从网页中提取我们想要的信息。

Python中使用

其实正则表达式不是Python独有的,它在其他编程语言中也可以使用,但是Python的re库提供了整个正则表达式的实现,利用re库我们就可以在Python中使用正则表达式来,在Python中写正则表达式几乎都是用的这个库。

下面我们就来了解下它的用法。

match()

在这里首先介绍第一个常用的匹配方法,match()方法,我们向这个方法传入要匹配的字符串以及正则表达式,就可以来检测这个正则表达式是否匹配字符串了。

match()方法会尝试从字符串的起始位置匹配正则表达式,如果匹配,就返回匹配成功的结果,如果不匹配,那就返回None。

我们用一个实例来感受一下:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>import re

content = ‘Hello 123 4567 World_This is a Regex Demo’

print(len(content))

result = re.match(‘^Hellosdddsd{4}sw{10}’, content)

print(result)

print(result. group ())

print(result.span())

</pre>

运行结果:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>41

<_sre.SRE_Match object; span=(0, 25), match=’Hello 123 4567 World_This’>

Hello 123 4567 World_This

(0, 25)

</pre>

在这里我们首先声明了一个字符串,包含英文字母、空白字符、数字等等内容,接下来我们写了一个正则表达式^Hellosdddsd{4}sw{10}来匹配这个长字符串。

开头的^是匹配字符串的开头,也就是以Hello开头,然后s匹配空白字符,用来匹配目标字符串的空格,d匹配数字,三个d匹配123,然后再写一个s匹配空格,后面还有4567,我们其实可以依然用四个d来匹配,但是这么写起来比较繁琐,所以在后面可以跟{4}代表匹配前面的字符四次,也就是匹配四个数字,这样也可以完成匹配,然后后面再紧接一个空白字符,然后w{10}匹配10个字母及下划线,正则表达式到此为止就结束了,我们注意到其实并没有把目标字符串匹配完,不过这样依然可以进行匹配,只不过匹配结果短一点而已。

我们调用match()方法,第一个参数传入了正则表达式,第二个参数传入了要匹配的字符串。

打印输出一下结果,可以看到结果是SRE_Match对象,证明成功匹配,它有两个方法,group()方法可以输出匹配到的内容,结果是Hello 123 4567 World_This,这恰好是我们正则表达式规则所匹配的内容,span()方法可以输出匹配的范围,结果是(0, 25),这个就是匹配到的结果字符串在原字符串中的位置范围。

通过上面的例子我们可以基本了解怎样在Python中怎样使用正则表达式来匹配一段文字。

匹配目标

刚才我们用了match()方法可以得到匹配到的字符串内容,但是如果我们想从字符串中提取一部分内容怎么办呢?就像最前面的实例一样,从一段文本中提取出邮件或电话号等内容。

在这里可以使用()括号来将我们想提取的子字符串括起来,()实际上就是标记了一个子表达式的开始和结束位置,被标记的每个子表达式会依次对应每一个分组,我们可以调用group()方法传入分组的索引即可获取提取的结果。

下面我们用一个实例感受一下:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>import re

content = ‘Hello 1234567 World_This is a Regex Demo’

result = re.match(‘^Hellos(d+)sWorld’, content)

print(result)

print(result.group())

print(result.group(1))

print(result.span())

</pre>

依然是前面的字符串,在这里我们想匹配这个字符串并且把其中的1234567提取出来,在这里我们将数字部分的正则表达式用()括起来,然后接下来调用了group(1)获取匹配结果。

运行结果如下:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”><_sre.SRE_Match object; span=(0, 19), match=’Hello 1234567 World’>

Hello 1234567 World

1234567

(0, 19)

</pre>

可以看到在结果中成功得到了1234567,我们获取用的是group(1),与group()有所不同,group()会输出完整的匹配结果,而group(1)会输出第一个被()包围的匹配结果,假如正则表达式后面还有()包括的内容,那么我们可以依次用group(2)、group(3)等来依次获取。

通用匹配

刚才我们写的正则表达式其实比较复杂,出现空白字符我们就写s匹配空白字符,出现数字我们就写d匹配数字,工作量非常大,其实完全没必要这么做,还有一个万能匹配可以用,也就是. ,.可以匹配任意字符(除换行符), 又代表匹配前面的字符无限次,所以它们组合在一起就可以匹配任意的字符了,有了它我们就不用挨个字符地匹配了。

所以接着上面的例子,我们可以改写一下正则表达式。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>import re

content = ‘Hello 123 4567 World_This is a Regex Demo’

result = re.match(‘^Hello.*Demo/pre>, content)

print(result)

print(result.group())

print(result.span())

</pre>

在这里我们将中间的部分直接省略,全部用.*来代替,最后加一个结尾字符串就好了,运行结果如下:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”><_sre.SRE_Match object; span=(0, 41), match=’Hello 123 4567 World_This is a Regex Demo’>

Hello 123 4567 World_This is a Regex Demo

(0, 41)

</pre>

可以看到group()方法输出了匹配的全部字符串,也就是说我们写的正则表达式匹配到了目标字符串的全部内容,span()方法输出(0, 41),是整个字符串的长度。

因此,我们可以在使用.*来简化正则表达式的书写。

贪婪匹配与非贪婪匹配

在使用上面的通用匹配.*的时候可能我们有时候匹配到的并不是想要的结果,我们看下面的例子:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>import re

content = ‘Hello 1234567 World_This is a Regex Demo’

result = re.match(‘^He. (d+). Demo/pre>, content)

print(result)

print(result.group(1))

</pre>

在这里我们依然是想获取中间的数字,所以中间我们依然写的是(d+),数字两侧由于内容比较杂乱,所以两侧我们想省略来写,都写. ,最后组成^He. (d+).*Demo$,看样子并没有什么问题,我们看下运行结果:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”><_sre.SRE_Match object; span=(0, 40), match=’Hello 1234567 World_This is a Regex Demo’>

7

</pre>

奇怪的事情发生了,我们只得到了7这个数字,这是怎么回事?

这里就涉及一个贪婪匹配与非贪婪匹配的原因了,贪婪匹配下,. 会匹配尽可能多的字符,我们的正则表达式中. 后面是d+,也就是至少一个数字,并没有指定具体多少个数字,所以.*就尽可能匹配多的字符,所以它把123456也匹配了,给d+留下一个可满足条件的数字7,所以d+得到的内容就只有数字7了。

但这样很明显会给我们的匹配带来很大的不便,有时候匹配结果会莫名其妙少了一部分内容。其实这里我们只需要使用非贪婪匹配匹配就好了,非贪婪匹配的写法是.*?,多了一个?,那么它可以达到怎样的效果?我们再用一个实例感受一下:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>import re

content = ‘Hello 1234567 World_This is a Regex Demo’

result = re.match(‘^He. ?(d+). Demo/pre>, content)

print(result)

print(result.group(1))

</pre>

在这里我们只是将第一个. 改成了. ?,转变为非贪婪匹配匹配。结果如下:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”><_sre.SRE_Match object; span=(0, 40), match=’Hello 1234567 World_This is a Regex Demo’>

1234567

</pre>

很好,这下我们就可以成功获取1234567了。原因可想而知,贪婪匹配是尽可能匹配多的字符,非贪婪匹配就是尽可能匹配少的字符,. ?之后是d+用来匹配数字,当. ?匹配到Hello后面的空白字符的时候,再往后的字符就是数字了,而d+恰好可以匹配,那么这里. ?就不再进行匹配,交给d+去匹配后面的数字。所以这样,. ?匹配了尽可能少的字符,d+的结果就是1234567了。

所以说,在做匹配的时候,字符串中间我们可以尽量使用非贪婪匹配来匹配,也就是用. ?来代替. ,以免出现匹配结果缺失的情况。

python学习关注我们企鹅qun: 839383765 各类入门学习资料免费分享哦!

但这里注意,如果匹配的结果在字符串结尾,.*?就有可能匹配不到任何内容了,因为它会匹配尽可能少的字符,例如:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>import re

content = ‘

result1 = re.match(‘http. ?comment/(. ?)’, content)

result2 = re.match(‘http. ?comment/(. )’, content)

print(‘result1’, result1.group(1))

print(‘result2’, result2.group(1))

</pre>

运行结果:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>result1

result2 kEraCN

</pre>

观察到. ?没有匹配到任何结果,而. 则尽量匹配多的内容,成功得到了匹配结果。

所以在这里好好体会一下贪婪匹配和非贪婪匹配的原理,对后面写正则表达式非常有帮助。

修饰符

正则表达式可以包含一些可选标志修饰符来控制匹配的模式。修饰符被指定为一个可选的标志。

我们用一个实例先来感受一下:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>import re

content = ”’Hello 1234567 World_This

is a Regex Demo

”’

result = re.match(‘^He. ?(d+). ?Demo/pre>, content)

print(result.group(1))

</pre>

和上面的例子相仿,我们在字符串中加了个换行符,正则表达式也是一样的来匹配其中的数字,看一下运行结果:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>AttributeError Traceback (most recent call last)

<ipython-input-18-c7d232b39645> in <module>()

5 ”’

6 result = re.match(‘^He. ?(d+). ?Demo/pre>, content)

—-> 7 print(result.group(1))

AttributeError: ‘NoneType’ object has no attribute ‘group’

</pre>

运行直接报错,也就是说正则表达式没有匹配到这个字符串,返回结果为None,而我们又调用了group()方法所以导致AttributeError。

那我们加了一个换行符为什么就匹配不到了呢?是因为.匹配的是除换行符之外的任意字符,当遇到换行符时,.*?就不能匹配了,所以导致匹配失败。

那么在这里我们只需要加一个修饰符re.S,即可修正这个错误。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>result = re.match(‘^He. ?(d+). ?Demo/pre>, content, re.S)

</pre>

在match()方法的第三个参数传入re.S,它的作用是使.匹配包括换行符在内的所有字符。

运行结果:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>1234567

</pre>

这个re.S在网页匹配中会经常用到,因为HTML节点经常会有换行,加上它我们就可以匹配节点与节点之间的换行了。

另外还有一些修饰符,在必要的情况下也可以使用:

修饰符描述

re.I使匹配对大小写不敏感

re.L做本地化识别(locale-aware)匹配

re.M多行匹配,影响 ^ 和 $

re.S使 . 匹配包括换行在内的所有字符

re.U根据Unicode字符集解析字符。这个标志影响 w, W, , B.

re.X该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解。

在网页匹配中较为常用的为re.S、re.I。

未完,接着下篇~

注: python学习关注我们企鹅qun: 8393 83765 各类入门学习资料免费分享哦!

文章来源:智云一二三科技

文章标题:Python正则表达式的使用方法(上)

文章地址:https://www.zhihuclub.com/176835.shtml

关于作者: 智云科技

热门文章

网站地图