substring in python

2019-07-30 23:21发布

i have strings with Following pattern in python :

2011-03-01 14:10:43 C:\Scan\raisoax.exe detected    Trojan.Win32.VBKrypt.agqw

how get substrings: C:\Scan\raisoax.exe and Trojan.Win32.VBKrypt.agqw

between string is tab

5条回答
Ridiculous、
2楼-- · 2019-07-31 00:04

just use the substring method of a python String.

s = r"2011-03-01 14:10:43 C:\Scan\raisoax.exe detected    Trojan.Win32.VBKrypt.agqw"
s.split("\t")

gets you

['2011-03-01 14:10:43 C:\\\\Scan\\raisoax.exe detected', 'Trojan.Win32.VBKrypt.agqw']
查看更多
神经病院院长
3楼-- · 2019-07-31 00:09

A solution using regexes:

s = "2011-03-01 14:10:43 C:\Scan\raisoax.exe detected    Trojan.Win32.VBKrypt.agqw"
reg = re.match(r"\S*\s\S*\s(.*)[^\ ] detected\s+(.*)",s)
file,name = reg.groups()

This will catch files with spaces in them as well. It will fail if you have files with " detected " in them. (you can add a forward assertion to fix that as well.

查看更多
Explosion°爆炸
4楼-- · 2019-07-31 00:15

You can use this package called "substring". Just type "pip install substring". You can get the substring by just mentioning the start and end characters/indices.

查看更多
可以哭但决不认输i
5楼-- · 2019-07-31 00:17
s = r"2011-03-01 14:10:43 C:\Scan\raisoax.exe detected    Trojan.Win32.VBKrypt.agqw"
v = s.split()
print v[-1] # gives you Trojan.Win32.VBKrypt.agqw
print v[-3] # gives you C:\Scan\raisoax.exe

To handle spaces in filenames try

print " ".join(v[2:-2])
查看更多
来,给爷笑一个
6楼-- · 2019-07-31 00:18

Use the re package. Something like

import re
s = r'2011-03-01 14:10:43 C:\Scan\raisoax.exe detected    Trojan.Win32.VBKrypt.agqw'
m = re.search('\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s(.+)\s+detected\s+(.+)', s)
print 'file: ' + m.group(1)
print 'error: ' + m.group(2)
查看更多
登录 后发表回答