[英]How to retrieve wanted string with re from lines
Tue Aug 21 17:02:26 2018 (gtgrhrthrhrhrthhhthrthrhrh)
fjfpjpgporejpejgjr[eh[[[jh[j[ej[[ej[ej[e]]]]
fkw[kgkeg[ekrk[ekg[kergk[erkg[eg[kg]
Tue Aug 21 17:31:06 2018 ( ijwejfwfjwpfjwf[[few[jjfwfefwfeffeww]]
fiowhfiweohewhfpwfhpfhpepwehfphpwhfpehfpwfh
f,wfpewfefewgpwpg,pewgp
Tue Aug 21 18:10:42 2018 ( reijpjfpjejferjfrejfpjefjer
k[pfk[epkf[kr[ek[ke[gkk]
r[g[keprkgpekg[rkg[pkg[ekg]
Above is an example of the content in the text file. 上面是文本文件中内容的示例。 I want to extract a string with
re
. 我想用
re
提取一个字符串。 How should I construct the findall
condition to achieve the expected result below? 我应该如何构造
findall
条件以达到下面的预期结果? I have tried the following: 我尝试了以下方法:
match=re.findall(r'[Tue\w]+2018$',data2)
but it is not working. 但它不起作用。 I understand that $ is the symbol for the end of the string.
我知道$是字符串结尾的符号。 How can I do it?
我该怎么做?
Expected Result is: 预期结果是:
Tue Aug 21 17:02:26 2018
Tue Aug 21 17:31:06 2018
Tue Aug 21 18:10:42 2018
.
.
.
Use the pattern: 使用模式:
^Tue.*?2018
^
Assert position beginning of line. ^
行首的位置。 Tue
Literal substring. Tue
文字子串。 .*?
Match anything lazily. 2018
Match literal substring. 2018
匹配文字子字符串。 Since you are working with a multiline string and you want to match pattern at the beginning of a string, you have to use the re.MULTILINE
flag. 由于您正在使用多行字符串,并且要在字符串开头匹配模式,因此必须使用
re.MULTILINE
标志。
import re
mystr="""
Tue Aug 21 17:02:26 2018 (gtgrhrthrhrhrthhhthrthrhrh)
fjfpjpgporejpejgjr[eh[[[jh[j[ej[[ej[ej[e]]]]
fkw[kgkeg[ekrk[ekg[kergk[erkg[eg[kg]
Tue Aug 21 17:31:06 2018 ( ijwejfwfjwpfjwf[[few[jjfwfefwfeffeww]]
fiowhfiweohewhfpwfhpfhpepwehfphpwhfpehfpwfh
f,wfpewfefewgpwpg,pewgp
Tue Aug 21 18:10:42 2018 ( reijpjfpjejferjfrejfpjefjer
k[pfk[epkf[kr[ek[ke[gkk]
r[g[keprkgpekg[rkg[pkg[ekg]
"""
print(re.findall(r'^Tue.*?2018',mystr,re.MULTILINE))
Prints: 打印:
['Tue Aug 21 17:02:26 2018', 'Tue Aug 21 17:31:06 2018', 'Tue Aug 21 18:10:42 2018']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.