I'm writing my first Python re code and i have some doubt regarding the Regular Expression. I have a variable which contains
a = "'PoE Port Info','1 up medium Auto Class Searching 0 0.0 0 0.0','10 up low User defined 4(W) Searching - 0.0 0 0.0'"
I need to extract:
'1 up medium Auto Class Searching 0 0.0 0 0.0'
and convert the string to list of string with all whitespaces removed
['1','up','medium','Auto Class','Searching','0','0.0','0','0.0']
similarly
'10 up low User defined 4(W) Searching - 0.0 0 0.0'
remove the whitespaces and convert to list
['10','up','low','User defined,'4(W)','Searching','-','0.0','0','0.0']
all the other remaining data in that string should not match.
My code:
a = "'PoE Port Info','1 up medium Auto Class Searching 0 0.0 0 0.0','10 up low User defined 4(W) Searching - 0.0 0 0.0'"
b= []
#split string with ,
a = a.split(",")
print(a)
for item in a:
c = re.findall(r"[0-9]+[\s+[a-z]+]*[0-9]+",item, re.I)
if c:
#Replace whitespace character to spaces
temp = re.sub(r'[\s]+',' ', c[0])
#print(temp)
b.append(temp.split(" "))
print(b)
This code is working but i'm facing issue at Regular expression. My current output:
[['1', 'up', 'medium', 'Auto', 'Class', 'Searching', '0'], ['10', 'up', 'low', 'User', 'defined', '4']]
Some please help me.
How to write the RE?
'at the start and end of the string and split on 2 or more whitespace chars[^\S\r\n]{2,}regex101.com/r/edL69K/1PoE Port Infonot be part of the result?PoE Port Infoshould not be part of the final result.