第1章 1.8 引入正则表达式

一、示例如下:

>>> import re

#导入正则表达式模块

>>> re.search(r'LOG', 'SOME LOGS')

<re.Match object; span=(5, 8), match='LOG'>

#在字符串中查找'LOG'

>>> re.search(r'^LOG', 'LOGS')

<re.Match object; span=(0, 3), match='LOG'>

#'^'是指查找'LOG'开头的字符串

>>> re.search(r'^LOG', 'SOME LOGS')

#该模块未找到'LOG'开头的字符串,返回None

>>> re.search(r'LOG$', 'SOME LOGS')

#'$'是指查找'LOG'结尾的字符串,未找到返回None

>>> re.search(r'LOG$', 'SOME LOG')

<re.Match object; span=(5, 8), match='LOG'>

#'$'是指查找'LOG'结尾的字符串

>>> STRING = 'something in the things she shows me'

>>> match = re.search(r'thing', STRING)

#在字符串中查找thing,只取第1个

>>> STRING[:match.start()], STRING[match.start():match.end()], STRING[match.end():]

('some', 'thing', ' in the things she shows me')

#该命令为分别取出匹配前的字符串、匹配的字符串和匹配后的字条串,其中match.start()为匹配的开始索引、match.end()为匹配的结束索引。

>>> match = re.search(r'\bthing', STRING)

#\b标志单词的开头或者结尾, 其中something不是单词的开头,而things是单词的开头

>>> STRING[:match.start()], STRING[match.start():match.end()], STRING[match.end():]

('something in the ', 'thing', 's she shows me')

#该命令为分别取出匹配前的字符串、匹配的字符串和匹配后的字条串

>>> re.search(r'[0123456789-]+', 'the phone number is 1234-567-890')

<re.Match object; span=(20, 32), match='1234-567-890'>

#该命令匹配包含'0'至'9'的数据和'-'符号,即匹配电话号码

>>> re.search(r'[0123456789-]+', 'the phone number is 1234-567-890').group()

'1234-567-890'

#group()取出匹配的字符串

>>> re.search(r'\S+@\S+', 'my email is email.123@test.com').group()

'email.123@test.com'

#'\S':标记除空格外的任何字符,包括特殊字符。该模式为提取电子邮件地址,比较严格的模式应该为r'([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)'

>>> re.search(r'([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)', 'my email is jonhn@smith@test.com')

<re.Match object; span=(18, 32), match='smith@test.com'>

>>> re.search(r'([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)', 'my email is email123@test.com')

<re.Match object; span=(12, 29), match='email123@test.com'>

>>> match = re.search(r'[0123456789-]+', 'the phone number is 1234-567-890')

>>> [int(n) for n in match.group().split('-')]

[1234, 567, 890]

#取出电话号码,然后以'-'分隔,并将生成器转换为列表。

 

可以使用一些工具交互式地检查正则表达式。在https://regex101.com可以找到一个免费的在线测试工具。

 

posted @ 2022-04-09 11:41  轻舞飞洋  阅读(35)  评论(0编辑  收藏  举报