[英]Cant extract substring from the string using regex in python
I want to extract the substring "login attempt [b'admin'/b'admin']" from the string:我想从字符串中提取 substring“登录尝试 [b'admin'/b'admin']”:
2021-05-06T00:00:15.921179Z [HoneyPotSSHTransport,1127,5.188.87.53] login attempt [b'admin'/b'admin'] succeeded.
But python returns the whole string.但是 python 返回整个字符串。 My code is:我的代码是:
import re
hand = open('cowrie.log')
outF = open("Usernames.txt", "w")
for line in hand:
if re.findall(r'login\sattempt\s\[[a-zA-z0-9]\'[a-zA-z0-9]+\'/[a-zA-z0-9]+\'[a-zA-z0-9]+\'\]', line):
print(line)
outF.write(line)
outF.write("\n")
outF.close()
Thanks in advance.提前致谢。 This is the LINK which contains the data from which I want to extract.这是包含我要从中提取数据的链接。
Your code states: if re.findall returns something, print the whole line.您的代码指出:如果 re.findall 返回某些内容,则打印整行。 But you should print the return from re.findall and write that as a string.但是您应该打印 re.findall 的返回并将其写为字符串。
Or use re.search if you expect a single match.或者,如果您期望单个匹配项,请使用 re.search。
Note that [Az]
matches more than [A-Za-z]
.请注意, [Az]
匹配的次数多于[A-Za-z]
。
import re
hand = open('cowrie.log')
outF = open("Usernames.txt", "w")
for line in hand:
res = re.search(r"login\sattempt\s\[[a-zA-Z0-9]'[a-zA-Z0-9]+'/[a-zA-Z0-9]+'[a-zA-Z0-9]+']", line)
if res:
outF.write(res.group())
outF.write("\n")
outF.close()
Usernames.txt now contains: Usernames.txt 现在包含:
login attempt [b'admin'/b'admin']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.