1

I want to build a RegEx that will match with a string of IPs separated by comma (,) OR string will have only *. String should not have both IP address & *

  1. Validate IP i.e 1.1.1.1 (Numbers and . dot char). Also, * alone is allowed

  2. * is present, no other IPs should be present.

This is the regex

(((25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)(,\n|,?))|(,*))

Testing string:

192.168.1.1,192.56.3.23,189.35.2.2,198.23.45.56,198.168.1.255

How do I check for *?

Kaspar Lee
  • 5,446
  • 4
  • 31
  • 54
punter
  • 460
  • 1
  • 6
  • 22
  • It seems we need more than one string to test. Please supply some more valid and invalid strings. Check [this regex demo](https://regex101.com/r/gS6eE8/1) - is that something like what you need? – Wiktor Stribiżew Apr 07 '16 at 14:35
  • 3
    @punter why exactly do you think a regular expression is the best way to verify the data? Would it not be simpler to just split the string on `,` characters and verify that each IP address is valid, and then for each IP address, split on `.` characters and very that each number or `*` is valid? Sometimes writing more lines of code is better than trying to write a single very clever line of code. – zzzzBov Apr 07 '16 at 14:43
  • Just check for `st == '*'`. As for IP addresses, regex is indeed not the best idea, there are some better methods described [here](http://stackoverflow.com/a/3462840/5629218). – Lav Apr 08 '16 at 14:23
  • Maybe [this regex will help](https://regex101.com/r/nA9tQ7/1)? – Wiktor Stribiżew Apr 12 '16 at 06:58
  • @WiktorStribiżew Yes, thanks! – punter Aug 17 '18 at 06:39

1 Answers1

3

You may use

^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|\*)(?:,\s*(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|\*))*$

See the regex demo

Expanded//verbose/free-spacing version:

^         # start of string
 (?:      # start of a grouping
  (?:     # start of another grouping
   (?:25[0-5]|2[0-4]\d|[01]?\d\d?)\. # First octet and .
   (?:25[0-5]|2[0-4]\d|[01]?\d\d?)\. # Second octet and .
   (?:25[0-5]|2[0-4]\d|[01]?\d\d?)\. # Third octet and .
   (?:25[0-5]|2[0-4]\d|[01]?\d\d?)   # Fourth octet
   |\*                               # or just a * char instead of an IP
  )                                  # end of another grouping
 )                                   # end of grouping
 (?:,\s*             # a group that will repeat 0+ times, matches , then 0+ whitespaces   
  (?:                # an IP matching grouping
   (?:25[0-5]|2[0-4]\d|[01]?\d\d?)\. # First octet and .
   (?:25[0-5]|2[0-4]\d|[01]?\d\d?)\. # Second octet and .
   (?:25[0-5]|2[0-4]\d|[01]?\d\d?)\. # Third octet and .
   (?:25[0-5]|2[0-4]\d|[01]?\d\d?)   # Fourth octet
  |\*)                               # Or a *
 )*                                  # ... zero or more times 
$                                    # end of string
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563