Python Forum
Regex to extract IPs between () not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex to extract IPs between () not working
#1
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)
Reply
#2
show runable snippet
Reply
#3
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
[]
>>>
Reply
#4
. 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:
Reply
#5
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')]
Reply
#6
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Alternative to regex to extract date from whole timestamp? Winfried 6 1,777 Nov-16-2022, 01:49 PM
Last Post: carecavoador
  regex pattern to extract relevant sentences Bubly 2 1,837 Jul-06-2021, 04:17 PM
Last Post: Bubly
  Who enjoys Py RegEx? re.sub() isn't working goodsignal 4 2,481 Jun-08-2020, 06:12 AM
Last Post: bowlofred
  regex to extract only yy or yyyy metalray 2 3,552 Jul-11-2018, 07:57 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020