Here’s an extended example from Friedl’s Mastering Regular Expressions, page 189, that covers many of the features described in this chapter. The problem is to fashion a regexp that will match any and only IP addresses or dotted quads: four numbers separated by three dots, with each number between 0 and 255.
First, we define a subregexp n0-255 that matches 0 through 255:
Note that n0-255 lists prefixes as preferred alternates, which is something we cautioned against in Alternation. However, since we intend to anchor this subregexp explicitly to force an overall match, the order of the alternates does not matter.
The first two alternates simply get all single- and double-digit numbers. Since 0-padding is allowed, we need to match both 1 and 01. We need to be careful when getting 3-digit numbers, since numbers above 255 must be excluded. So we fashion alternates to get 000 through 199, then 200 through 249, and finally 250 through 255.
An IP-address is a string that consists of four n0-255s with three dots separating them.
Let’s try it out:
|> (regexp-match (pregexp ip-re1) "184.108.40.206")|
|> (regexp-match (pregexp ip-re1) "220.127.116.115")|
which is fine, except that we also have
|> (regexp-match (pregexp ip-re1) "0.00.000.00")|
All-zero sequences are not valid IP addresses! Lookahead to the rescue. Before starting to match ip-re1, we look ahead to ensure we don’t have all zeros. We could use positive lookahead to ensure there is a digit other than zero.
Or we could use negative lookahead to ensure that what’s ahead isn’t composed of only zeros and dots.
The regexp ip-re will match all and only valid IP addresses.
|> (regexp-match ip-re "18.104.22.168")|
|> (regexp-match ip-re "0.0.0.0")|