Match words before square bracket and extract the value

时间:2022-09-13 11:42:06

Hi i have an log data

嗨,我有一个日志数据

IPADDRESS['192.168.0.10'] - - PlATFORM['windows'] - - BROWSER['chrome'] - - EPOCH['1402049518'] - - HEXA['0x3c0593ee']

I want to extract the corresponding value inside the bracket using regular expression

我想使用正则表达式提取括号内的相应值

for example (this does not work)

例如(这不起作用)

   ipaddress = re.findall(r"\[(IPADDRESS[^]]*)",output)

ouput:

输出继电器:

ipaddress = 192.168.0.10

2 个解决方案

#1


2  

You can simply get all the elements as a dictionary like this

您可以简单地将所有元素作为字典获取

print dict(re.findall(r'([a-zA-Z]+)\[\'(.*?)\'\]', data))

Output

产量

{'BROWSER': 'chrome',
 'EPOCH': '1402049518',
 'HEXA': '0x3c0593ee',
 'IPADDRESS': '192.168.0.10',
 'PlATFORM': 'windows'}

#2


0  

s1 = "IPADDRESS['192.168.0.10'] - - PlATFORM['windows'] - - BROWSER['chrome'] - - EPOCH['1402049518'] - - HEXA['0x3c0593ee']"

import re

print re.findall('\[\'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\'\]',s1)[0]
192.168.0.10

To get all in a list:

要获得列表中的所有内容:

s1 = "IPADDRESS['192.168.0.10'] - - PlATFORM['windows'] - - BROWSER['chrome'] - - EPOCH['1402049518'] - - HEXA['0x3c0593ee']"

print re.findall('\[\'(.*?)\'\]',s1)

['192.168.0.10', 'windows', 'chrome', '1402049518', '0x3c0593ee']


result=re.findall('\[\'(.*?)\'\]',s1)

ipaddress, platform, browser, epoch, hexa = result # assign all variables

print "ipaddress = {}, platform = {}, browser = {}, epoch = {}, hexa = {}".format(ipaddress,platform,browser,epoch,hexa)


 ipaddress = 192.168.0.10, platform = windows, browser = chrome, epoch = 1402049518, hexa = 0x3c0593ee

#1


2  

You can simply get all the elements as a dictionary like this

您可以简单地将所有元素作为字典获取

print dict(re.findall(r'([a-zA-Z]+)\[\'(.*?)\'\]', data))

Output

产量

{'BROWSER': 'chrome',
 'EPOCH': '1402049518',
 'HEXA': '0x3c0593ee',
 'IPADDRESS': '192.168.0.10',
 'PlATFORM': 'windows'}

#2


0  

s1 = "IPADDRESS['192.168.0.10'] - - PlATFORM['windows'] - - BROWSER['chrome'] - - EPOCH['1402049518'] - - HEXA['0x3c0593ee']"

import re

print re.findall('\[\'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\'\]',s1)[0]
192.168.0.10

To get all in a list:

要获得列表中的所有内容:

s1 = "IPADDRESS['192.168.0.10'] - - PlATFORM['windows'] - - BROWSER['chrome'] - - EPOCH['1402049518'] - - HEXA['0x3c0593ee']"

print re.findall('\[\'(.*?)\'\]',s1)

['192.168.0.10', 'windows', 'chrome', '1402049518', '0x3c0593ee']


result=re.findall('\[\'(.*?)\'\]',s1)

ipaddress, platform, browser, epoch, hexa = result # assign all variables

print "ipaddress = {}, platform = {}, browser = {}, epoch = {}, hexa = {}".format(ipaddress,platform,browser,epoch,hexa)


 ipaddress = 192.168.0.10, platform = windows, browser = chrome, epoch = 1402049518, hexa = 0x3c0593ee