1

I want to match ip address which comes in below format, Example : .1.9.2...1.6.8...2.3.4...1.3.4. Not sure how to match the dots between the digits.

  • 1
    Possible duplicate of [Regular Expression to match a dot](https://stackoverflow.com/questions/13989640/regular-expression-to-match-a-dot) – anothernode Jun 05 '18 at 15:43

2 Answers2

1

Figured out the solution.. So the basic issue was how the base64decoding is done. It varies depending on the OS. So on some machines it was decoding and showing the ip in form [1.9.2...1.6.8...2.5.4...1.2.2.] for such input the above code works perfectly. But i observed in some machine with same code n python version the output came in different format, so on such cases the above code would fail. The best possible solution was to decode the base64 string to consistent format. After alot of hit n try, observed that converting the encoding ndecoding to 'utf-16' resulted in providing a better readable string with now the ip showing up as normal:192.168.254.122 so on this you may use a basic ip match regex and move forward.

0

This should be what you're looking for:

/([0-9]\.[0-9]\.[0-9])|([0-9]\.[0-9])|([0-9])/g

https://regexr.com/3qihj


Explanation:

There are three capture groups (denoted by parentheses): One for three digits, one for two digits and one for one digit. These are then OR'd using a pipe | character.

Matching the dots between the digits is done using a backslash escape, as a dot character in regex is a meta-character to match any character except a newline.

The regex engine should output an array with a length of 4, corresponding to all four parts of the IP address. The single dots in the middle will be included in each result, the triple dot separators however, will not.

Update with Python Script

I've changed the regex slightly to work with Python.
Works in https://www.tutorialspoint.com/execute_python_online.php

import re
ip = 'W.i.n.3.2._.O.p.e.r.a.t.i.n.g.)h.t.t.p.:././.1.2...1.6.8...4...1.3.5./'

ip = ip.split("h.t.t.p.:././.")[1]
result = re.findall('([0-9]\.[0-9]\.[0-9])|([0-9]\.[0-9])|([0-9])', ip)
ipaddress = ""
for x in range(0, len(result)):
    if len(result[x][0]) > 0:
        ipaddress += result[x][0].replace('.','')
    if len(result[x][1]) > 0:
        ipaddress += result[x][1].replace('.','')
    if len(result[x][2]) > 0:
        ipaddress += result[x][2].replace('.','')
    if(x < len(result) -1):
        ipaddress+= '.'
print (ipaddress) 
Will Jones
  • 1,861
  • 13
  • 24
  • Well i want to print out the ip address out from a string in the form: h.t.t.p.:././.1.9.2...1.6.8...4.5...7.8./ i want just the ip: 192.168.45.78 but above regex is not matching&printing out in desired form. – sharat chandran Jun 05 '18 at 21:01
  • I've edited my answer to include a python script to do just this – Will Jones Jun 05 '18 at 21:32
  • Wow! It works great. But it will fail if there were other digits in the string. https://www.tutorialspoint.com/execute_python_online.php If suppose i keep ip = 'W.i.n.3.2._.O.p.e.r.a.t.i.n.g.)h.t.t.p.:././.1.2...1.6.8...4...1.3.5./' this will give 32.12.168.4.135, it might need to start my regex find from /./. – sharat chandran Jun 05 '18 at 22:12
  • Ah yes, that won't take in to account additional digits before or after the IP. The easiest way of doing that would be to split your input string by "h.t.t.p.:././." and run the regex on the result. – Will Jones Jun 06 '18 at 10:35
  • Yup.. Thanks for the help. – sharat chandran Jun 06 '18 at 22:19
  • Landed in all sorts of issues while running the code on py3.6 – sharat chandran Jun 07 '18 at 05:54
  • I've just run the script above in py3.6.5, the only changes required were wrapping the ipaddress variable in parentheses for the print function. I've added the split function as well and this appears to work. Can you edit your post to include the script you're using? – Will Jones Jun 07 '18 at 10:55
  • Yup, it seems to work on tutorialspoint, but i even tried the code on py2.7.13, but the issue is now the ip we need to match is in standard format; 120.25.148.202. I am trying to decode base64 to utf-8 and take out the ip from it. Somehow its working fine on tutorialpoint but not on my machine.https://www.tutorialspoint.com/execute_python_online.php – sharat chandran Jun 07 '18 at 11:09
  • Need a regex to match 1 2 0 . 2 5 . 1 4 8 . 2 0 2 and give me correct format 120.25.148.202 this would solve the issue, after running the previous regex i am getting in below format [('', ", '1'), (", ", '2'), (", ", '0').........so on] – sharat chandran Jun 07 '18 at 13:17
  • In that link you're not using the additional part of the script edited into my answer that splits the string at the http part. That script spits out 32.111.230.229.226 as the IP, which has the extra 32 at the start. – Will Jones Jun 07 '18 at 19:29