-1

I wrote the following regexp to match IP address,

x = 'IP is 200.185.24.24'
y = re.findall('([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-2][0-3])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])',x)

I am getting the following answer,

>>> y
[('200', '185', '24', '2')]

I have 2 questions here,

  1. Why my mast octet is failing here. Rest works fine.
  2. When you see my pattern above, I have repeated the same to match 2nd,3rd and 4th octet of an IP address. Is there a easy way to represent this repeated patterns. I guess \d in tcl is for repeat (not sure). I am looking for similar in python.

Appreciate your inputs.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
Raj
  • 11
  • 1
  • There are several example regexes for matching IP addresses in The Regex Cookbook ch 7 at https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9780596802837/ch07s16.html. –  Aug 04 '15 at 05:54

1 Answers1

0

Turn all the capturing groups present in your regex to non-capturing groups. And also it's better to use word boundaries or otherwise you have to reverse the pattern like (?:large-pattern|medium-pattern|small-pattern)

y = re.findall('\b(?:[1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-2][0-3])\.(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\b',x)

You get the above output mainly because of re.findall function which gives the first preference to groups rather than matches. If there is no groups present then it gives the prefernce to matches.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Thanks Avinash. I tried yours, but is returns an empty list, >>> x = 'IP is 200.185.24.24' >>> y = re.findall('\b(?:[1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-2][0-3])\.(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\b',x) >>> >>> y [] ................. Appreciate your inputs. – Raj Aug 04 '15 at 07:19