def check_private_network(ip) priv_re = /^(192\.168\.|10\.|172.16)/ ret = false puts "Checking #{ip}" result = (ip =~ priv_re) if result ret = true end return ret end ip_re = "((?:[0-9]{1,3}\.){3}(?:[0-9]{1,3}))" fun_re = Regexp.new("(#{ip_re})(?{ check_private_network($g[0])})"); # this set matches correctly, identifies i0 and i2 as private ips = ["192.168.0.1", "1.2.3.4", "10.8.3.44", "73.55.244.2"] # output is: # Checking 192.168.0.1 # 192.168.0.1 is a private net # Checking 1.2.3.4.0.1 # Checking 10.8.3.44 # 10.8.3.44 is a private net # Checking 73.55.244.2 # Checking 73.55.244 # # No, I'm not sure why the 2nd check is '1.2.3.4.0.1'. A bug on my part likely. # No, I'm not sure why the last check is not a valid IP. # This one should identify 192.168.0.1 as a private ip # but maybe oniguruma's optimizations break backtracking? #ips = ["foo bar 1.2.3.4 192.168.0.1 1.2.3.4"] # output is # Checking 1.2.3.4 # # It should 'fail' on 1.2.3.4 and move on to the 192.168 address. # The expected output would be: # Checking 1.2.3.4 # Checking 192.168.0.1 # 192.168.0.1 is a private net # And $1 should be 192.168.0.1, but it isn't. Bugs for me! ips.each { |x| y = fun_re.match(x) if y.to_s.length > 0 puts "#{y} is a private net" end } ###