如何获取带有匹配子串的句子/字符串
How to get the sentence / string with the matched substring
如果在文本文件中给出以下内容,
Text1.txt
已注册:[10-01-21] 已注册:[12-02-21]
已批准:[03-03-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))
试试这个:
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)
如果在文本文件中给出以下内容,
Text1.txt
已注册:[10-01-21] 已注册:[12-02-21]
已批准:[03-03-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))
试试这个:
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)