Posts: 25
Threads: 11
Joined: Apr 2019
Hello, can you please help me retrieve IP addresses between (). Like this:
(1.1.1.1,230.1.1.1)
Notice the below is not working since it is capturing text like this:
match=re.findall(r'\(([0-9].+)\),string)
if match:
match = my_s_g_ip
(...) It would also capture:
(source,group)
Posts: 12,022
Threads: 484
Joined: Sep 2016
Posts: 25
Threads: 11
Joined: Apr 2019
To be more precise, I am puzzled why the below doesn't match (50.50.50.22,230.207.200.20). Any ideas?
s = """
rate 230.207.200.1', '', ' (50.50.50.11,230.207.200.1)', ' Incoming rate : 1013 / 8127181 (0/5/CPU0)', ' Outgoing rate : ', ' Node : 5/6/CPU0 : 933 / 15828597. >> it shows double as O-list is on 2 PSE’s.', ' Node : 0/6/CPU0 : 1032 / 7966755 ', ' Node : 0/9/CPU0 : 1035 / 15828597. >> it shows double as O-list is on 2 PSE’s.', ' Node : 0/1/CPU0 : 998 / 7966755 ', ' Node : 5/6/CPU0 : 999 / 15828597. >> it shows double as O-list is on 2 PSE’s.', ' Node : 0/6/CPU0 : 899 / 7966755 ', ' Node : 0/9/CPU0 : 1012 / 15828597. >> it shows double as O-list is on 2 PSE’s.', ' Node : 0/1/CPU0 : 999 / 7966755 ', ' [b](50.50.50.22,230.207.200.22)[/b]', ' Incoming rate : 2222 / 8127181 (0/5/CPU0)', ' Outgoing rate : ', ' Node : 1/3/CPU0 : 933 / 15828597. >> it shows double as O-list is on 2 PSE’s.', ' Node : 1/2/CPU0 : 1032 / 7966755 ', '', ' ', ' ', ' '] """
>>> match = re.findall(r'([0-9].[0-9].[0-9].[0-9],[0-9].[0-9].[0-9].[0-9].[0-9])', s)
>>> match
[]
>>>
>>> match = re.findall(r'([0-9].[0-9].[0-9].[0-9],[0-9].[0-9].[0-9].[0-9].[0-9])', s)
>>> match
[]
>>>
Posts: 5,151
Threads: 396
Joined: Sep 2016
Apr-12-2019, 02:06 AM
(This post was last modified: Apr-12-2019, 02:06 AM by metulburr.)
. is a wildcard in regex to stand for every character
try this regex
\(\d{1,3}(\.\d{1,3}){3},\d{1,3}(\.\d{1,3}){3}\) https://www.regextester.com/?fam=108886
Recommended Tutorials:
Posts: 25
Threads: 11
Joined: Apr 2019
Apr-12-2019, 02:36 AM
(This post was last modified: Apr-12-2019, 02:37 AM by mrapple2020.)
It improved. At least I get match. But any idea why I am getting only incomplete like below? I try on rubular.com and it appears to work. But when I apply it to my text it is different:
>>>
>>> s
" #show mfib route rate 230.207.200.1', '', ' (50.50.50.11,230.207.200.1)', "
>>>for item in s:
match = re.findall(r'\(\d{1,3}(\.\d{1,3}){3},\d{1,3}(\.\d{1,3}){3}\)', item)
if match:
print(match)
...
[('.11', '.1')]
[('.11', '.1')]
[('.11', '.1')]
[('.11', '.1')]
[('.11', '.1')]
[('.11', '.1')]
[('.11', '.1')]
[('.11', '.1')]
Posts: 2,121
Threads: 10
Joined: May 2017
Are you iterating over a string?
Check, if item is really a whole line.
Then try this regex:
ip_block = r'((?:\d{1,3}\.){3}\d{1,3})' # ip is a group
ip_line = rf'\({ip_block},{ip_block}\)' # parenthesis around ip1,ip2 The second one is a format string. It takes the ip_block twice spitted by comma and surrounded by parenthesis.
Finally the resulting regex:
Output: \(((?:\d{1,3}\.){3}\d{1,3}),((?:\d{1,3}\.){3}\d{1,3})\)
Then iterate line by line over the string or file-object.
Use re.match(the_regex, line).
If re.match: -> then you have match.group(1) for the left ip and match.group(2) for the right ip.
|