[英]How to get the sentence / string with the matched substring
If the following content is given in a text file,如果在文本文件中给出以下内容,
Registered: [10-01-21] Left: [12-02-21]已注册:[10-01-21] 已注册:[12-02-21]
Approved: [03-03-21] Name: Ann批准:[03-03-21] 姓名:安
Ann submitted documents.安提交了文件。 Ann checked [12-04-21]安检查 [12-04-21]
I need to get the output as :
Registered : [10-01-21]
Left : [12-02-21]
Approved : [03-03-21]
checked [12-04-21]
As the substring, we can give a regex to validate the date format.
import re
f1= open("Text1.txt")
string=f1.read()
found_p1=re.findall(r"(.*)\[[0-9]{2}[-][0-9][2][-][0-9]{2}\](.*)" ,string)
k=[ ]
for i in found_p1:
i=re.sub(r"|',|'(|)", "", str(i))
try this:尝试这个:
import re
f = open("Text1.txt")
string = f.read()
pattern = re.compile("(\w+\s\[\d{2}[-]\d{2}[-]\d{2}\]|\w+\s[:][\s]\[\d{2}[-]\d{2}[-]\d{2}\])")
m = pattern.findall(string)
for i in m:
print(i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.