自动化之旅:正则表达式、命令行参数与任务自动化
1. 深入了解正则表达式
正则表达式是处理文本信息的强大工具,下面将详细介绍如何使用正则表达式进行文本匹配和信息提取。
1.1 操作步骤
1.1.1 导入模块
import re
1.1.2 匹配电话号码模式
match = re.search(r'the phone number is ([\d-]+)', '37: the phone number is 1234-567-890')
print(match.group()) # 'the phone number is 1234-567-890'
print(match.group(1)) # '1234-567-890'
1.1.3 编译模式并捕获不区分大小写的模式
pattern = re.compile(r'The answer to question (\w+) is (yes|no)', re.IGNORECASE)
result = pattern.search('Naturally, the answer to question 3b is YES')
print(result) # <_sre.SRE_Match object; span=(10, 42), match='the answer to question
超级会员免费看
订阅专栏 解锁全文
951

被折叠的 条评论
为什么被折叠?



