python - 如何使用正则表达式 python3 从文本文件中查找字符串?
问题描述
如何使用正则表达式查找单词?
文件.txt
CLIENT BILL
BILL
FINAL BILL
TOTAL BILL
蟒蛇代码
import re
with open('file.txt','r') as f:
input_file = f.readlines()
for i in input_file:
s = re.findall(r'L/sBILL',i)
print(s)
预期输出:
FINAL BILL
TOTAL BILL
解决方案
用于re.match
此,另一种模式:
for i in input_file:
if re.match('\w+(s|L) BILL', i.rstrip()):
print(i.rstrip())
输出:
FINAL BILL
TOTAL BILL
推荐阅读
- c - 编译依赖于外部库 Opus 和 Faac 的 WebAssembly 程序
- ios - IOS:导入笔记的API
- c# - Input.GetKeyDown 在我调用两次时不起作用
- amazon-cloudformation - 使用 AWS CDK 为 AWS API 网关启用 CORS
- mysql - MYSQL 从 3 个不同的字段中抓取 DISTINCT 行
- azure - 为什么 Azure Scale-Out 会导致 Web 应用程序崩溃
- php - Wordpress 插件开发 - 文件上传:如何允许指定的文件类型?
- react-native - 当我有 PanResponder 时,如何保持滑动以返回?
- azure - LUIS 列表实体
- javascript - React Native:使用 UI 工具包进行设计好不好?