簡體   English   中英

如何使用正則表達式python3從文本文件中找到字符串?

[英]How to find a string from text file by using regex python3?

如何使用正則表達式查找單詞?

file.txt

CLIENT BILL
BILL
FINAL BILL
TOTAL BILL

python代碼

  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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM