Determine if a string is a valid IPv4 address. The standard form of such an address is a dotted quad or dotted decimal string. That is, 4 decimal numbers separated by periods, each number ranging from 0 to 255 The \d is double-scaped so that the slash gets passed on the regex.
#!/usr/local/bin/ruby
# Determine if a string is a valid IPv4 address. The standard
# form of such an address is a dotted quad or dotted decimal
# string. That is, 4 decimal numbers separated by periods,
# each number ranging from 0 to 255 The \d is double-scaped
# so that the slash gets passed on the regex.
num = "(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])" # defined as string
num = /(\d|[01]?\d\d|2[0-4]\d|25[0-5])/ # defined as regex
pat = "^(#{num}\.){3}#{num}$"
ip_pat = Regexp.new(pat)
puts ip_pat.class
ipl = "127.0.0.1"
puts ipl.class
# "=~" is a matching operator with respect to regular
# expressions; it returns the position in a string where a
# match was found, or nil if the pattern did not match.
if /\w/ =~ "this"
puts "yes"
else
puts "no"
end