专栏名称: Python爱好者社区
人生苦短,我用Python。分享Python相关的技术文章、工具资源、精选课程、视频教程、热点资讯、学习资料等。每天自动更新和推送。
目录
相关文章推荐
Python爱好者社区  ·  李飞飞:若我夺下诺贝尔奖,定以中国人身份去领 ·  2 天前  
Python中文社区  ·  狂暴大牛市该买点啥?用量化选股抓住机会 ·  4 天前  
Python中文社区  ·  牛回速归!央行降息降准,万亿重磅利好来袭 ·  6 天前  
Python爱好者社区  ·  yyds!Kaggle教程 ·  5 天前  
Python爱好者社区  ·  Grok-2,彻底炸了。。。 ·  1 周前  
51好读  ›  专栏  ›  Python爱好者社区

关于正则表达式的5个小贴士

Python爱好者社区  · 公众号  · Python  · 2017-12-28 19:13

正文

作者:刘志军,6年+Python使用经验, 高级开发工程师,目前在互联网医疗行业从事Web系统构架工作

个人公众号:Python之禅(微信ID:vttalk)


正则表达式是一个非常强大的处理字符工具,但有时可读性很差、晦涩难懂,Jamie Zawinski 说道:

Some people, when confronted with a problem, think, “I know, I’ll use regular expressions.” Now they have two problems.

本来是一个问题,引入正则表达式之后就成了两个问题。其实并不是任何场景都需要正则表达式。在简单场景,能用字符串自己提供的方法解决问题就没必要用正则表达式,比如字符替换

>>> import re
>>> text = 'java is most popular language'

>>> re.sub(r'java', 'python', text)
'python is most popular language'

# good
>>> text.replace("java", "python")
'python is most popular language'

判断字符串是否以某字符开头

>>> re.match(r"^java", text)
<_sre.sre_match object at class="" style="margin: 0px;padding: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;color: rgb(222, 147, 95);">0x000000000471D578>
>>> text.startwith("java")

# good
>>> text.startswith("java")
True

re.match() 与 re.search()

re.match 从字符串的起始位置匹配,如果没匹配成功就不再往后匹配,返回 None。而 search 虽然也是从起始位置开始匹配,但是如果在起始位置没有匹配,就继续往后匹配,直到匹配为止,如果匹配到字符串末尾都没有匹配则返回 None

>>> text = "java is most popular langauge"
>>> re.match("most", text) # 没匹配

# bad
>>> re.match(".*most", text)
<_sre.sre_match object at class="" style="margin: 0px;padding: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;color: rgb(222, 147, 95);">0x0000000004CCD578>

# good
>>> re.search("most", text)
<_sre.sre_match object at class="" style="margin: 0px;padding: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;color: rgb(222, 147, 95);">0x000000000471D578>

不分组的括号

我们知道正则表达式中括号可以用于分组提取,有时我们并不希望括号用于分组该怎么办,答案是使用 (?:),看一个例子,用正则表达式提取URL中的各个组成部分

rex = r'^(http[s]?)://([^/\s]+)([/\w\-\.]+[^#?\s]*)?(?:\?([^#]*))?(?:#(.*))?$'
print(re.match(rex, url).groups())
>>> ('http',
    'www.example.com',
    '/path/to/myfile.html',
    'key1=value1&key2=value2',
    'SomewhereInTheDocument')

上面虽然写了7对括号,但其实只有5个分组。下面是不使用 ?:,出现了 7 组数据

rex = r'^(http[s]?)://([^/\s]+)([/\w\-\.]+[^#?\s]*)?(\?([^#]*))?(#(.*))?$'
print(re.match(rex, url).groups())
>>>('http',
   'www.example.com',
   '/path/to/myfile.html',
   '?key1=value1&key2=value2',
   'key1=value1&key2=value2',
   '#SomewhereInTheDocument',
   'SomewhereInTheDocument')

贪婪匹配

正则表达式默认是贪婪匹配的,也就是说它会在满足匹配条件的情况下尽可能多的匹配字符,例如这里有一段话:

html = """

Today a quick article on a nic

Read more ...

"""

里面有两对

标签,如果你只想匹配第一对,使用

>>> re.search("

.*

"
, html)
>>> m = re.search("

.*

"
, html)
>>> m.group()
'

Today a quick article on a nic

Read more ...

'

>>>

.*

 会从第一个

开始,匹配到最后一个

,如果要想尽可能少匹配则可以在元字符后面加 ?
>>> m = re.search("

.*?

"
, html)
>>> m.group()
'

Today a quick article on a nic

'

Python爱好者社区历史文章大合集

Python爱好者社区历史文章列表(每周append更新一次)

福利:文末扫码立刻关注公众号,“Python爱好者社区”,开始学习Python课程:

关注后在公众号内回复课程即可获取:

0.小编的Python入门视频课程!!!

1.崔老师爬虫实战案例免费学习视频。

2.丘老师数据科学入门指导免费学习视频。

3.陈老师数据分析报告制作免费学习视频。

4.玩转大数据分析!Spark2.X+Python 精华实战课程免费学习视频。

5.丘老师Python网络爬虫实战免费学习视频。