I am trying to create a list of dictionaries using regex positive lookbehind. I tried two different codes:
Variation 1
string = '146.204.224.152 - lubo233'
for item in re.finditer( "(?P<host>[0-9]*[.][0-9]*[.][0-9]*[.][0-9]*)(?P<user_name>(?<= - )[a-z]*[0-9]*)", string ):
print(item.groupdict())
Variation 2
string = '146.204.224.152 - lubo233'
for item in re.finditer( "(?P<host>[0-9]*[.][0-9]*[.][0-9]*[.][0-9]*)(?<= - )(?P<user_name>[a-z]*[0-9]*)", string ):
print(item.groupdict())
Desired Output
{'host': '146.204.224.152', 'user_name': 'lubo233'}
Question/Issue
In both cases, I am unable to eliminate the substring " - ".
The use of positive lookbehind (?<= - ) renders my code wrong.
Can anyone assist to identify my mistake? Thanks.
I'd suggest you remove the positive lookbehind and just put the join character normally, between each parts
Also some improvements
\.instead of[.][0-9]{,3}instead of[0-9]*(?:\.[0-9]{,3}){3}instead of\.[0-9]{,3}\.[0-9]{,3}\.[0-9]{,3}Add a
.*along with the-to handle any word that could be there