How to convert my 'split' code into regex

Viewed 63

I have a raw.txt which is below.

ok: [192.168.1.1] => {
    "OS": "Ubuntu(Core) "
}
ok: [192.168.1.2] => {
    "OS": "Ubuntu (Core) "  
}
ok: [192.168.1.3] => {
    "OS": "CentOS (Core) "
}
ok: [192.168.1.3] => {
    "OS":"CentOS (Core) "  
}
ok: [192.168.1.5] => {
    "OS": "Red Hat(Core) "
}
ok: [192.168.1.6] => {
    "OS": "CentOS (Core) "  
}

My Python Code is below how to covert into desirable out

f = open(r'raw.txt', 'r')
s = f.read()
list1 = s.split('\n')
ip_list = []
os_list = []
for i in list1[::3]:
    ip_list.append(i)
for i in list1[1::3]:
    os_list.append(i)
y = [z[10:25] for z in os_list]
os_l = [x.strip(' ').replace('"','').replace(' ','') for x in y]
ip_l = [z[5:18] for z in ip_list]
ip_l_rep = [x.strip(' ').replace(']','') for x in ip_l]
{ip_l_rep[n]:os_l[n] for n in range(len(os_l))}

My Output and expected is below

{'192.168.1.1': 'Ubuntu(Core)',
 '192.168.1.2': 'Ubuntu(Core)',
 '192.168.1.3': 'CentOS(Core)',
 '192.168.1.5': 'RedHat(Core)',
 '192.168.1.6': 'CentOS(Core)'}

Due to multiple operations are using in this program I decided to write with help of regex. I wrote some pseudo code but not successful. like for extracting \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}.

Any enhance of my code also appreciated

2 Answers

You can use a regex to catch what is between [] and then after "OS": ":

import re
input = """
ok: [192.168.1.1] => {
    "OS": "Ubuntu(Core) "
}
ok: [192.168.1.2] => {
    "OS": "Ubuntu (Core) "
}
ok: [192.168.1.3] => {
    "OS": "CentOS (Core) "
}
ok: [192.168.1.3] => {
    "OS":"CentOS (Core) "
}
ok: [192.168.1.5] => {
    "OS": "Red Hat(Core) "
}
ok: [192.168.1.6] => {
    "OS": "CentOS (Core) "
}
"""
items = re.findall(r'\[(.*?)\].*?"OS": "(.*?)"', input, flags=re.S)
data = dict(items)  # only works as you have 2 items (IP, OSTYPE)

print(data)
# output: {'192.168.1.1': 'Ubuntu(Core) ', '192.168.1.2': 'Ubuntu (Core) ', '192.168.1.3': 'Red Hat(Core) ', '192.168.1.6': 'CentOS (Core) '}

This strips unneeded space from the text of between quotes ":

import re

f = open(r'raw.txt', 'r')
text = f.read()
f.close()

pattern = r'\[(.+?)\].+?:\s*"\s*(.+?)\s*"'
result = dict(re.findall(pattern, text, flags=re.DOTALL))
print(result)
# {'192.168.1.1': 'Ubuntu(Core)', '192.168.1.2': 'Ubuntu (Core)', '192.168.1.3': 'CentOS (Core)', '192.168.1.5': 'Red Hat(Core)', '192.168.1.6': 'CentOS (Core)'}
Related