-2

I have the following string:

DNS Servers . . . . . . . . . . . : 10.204.127.11
                                    10.207.2.50
                                    10.200.10.6

I'm trying to build a RegEx to parse out N number of IP addresses.

I understand there is an allegedly duplicate post here, but it is not working with the following RegEx:
http://regexr.com?321nu

^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$

p.s. sorry, a colleague of mine started this post on my account for me while I was away. I suppose I've added more information here. Feel free to unbury me.

Community
  • 1
  • 1
pixelbobby
  • 4,368
  • 5
  • 29
  • 49
  • 3
    What platform/language? Unix/bash? Perl? Python? – Qiau Aug 31 '12 at 14:07
  • possible duplicate of [Regular expression to match hostname or IP Address?](http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address) – Katriel Sep 02 '12 at 23:06
  • @Qiau, this is in HP Operations Manager which is compatible with Perl and Java type RegEx. This is not tagged with either because I didn't want SO'ers responding in Java or Perl similiar to one of my previous questions. – pixelbobby Sep 04 '12 at 14:21

2 Answers2

1

How about ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+) ?

As some numbers should not be allowed, better but not perfect pattern would be:

(1[0-9]{0,2}|2[0-9]{0,1}|2[0-4][0-9]|25[0-5]|[3-9][0-9]?)(\.(0|1[0-9]{0,2}|2[0-9]{0,1}|2[0-4][0-9]|25[0-5]|[3-9][0-9]?)){3}
Ωmega
  • 42,614
  • 34
  • 134
  • 203
-1

If you have exactly three IP addresses (see at Regexr) use \1,\2 and \3 from this regex:

^\D*(.*)\s*(.*)\s*(.*)$

If you have 1 to 3 IP addresses (see at Regexr) use \1,\2 and \3 from this regex (check if \2 and \3 exist before use):

^\D*(?:(.*)(?:\s*(.*)(?:\s*(.*))?)?)$
mmdemirbas
  • 9,060
  • 5
  • 45
  • 53