Regular expression to validate IPv4 address in ruby

regex, ruby

Solution

block = /\d{,2}|1\d{2}|2[0-4]\d|25[0-5]/
re = /\A#{block}\.#{block}\.#{block}\.#{block}\z/

re =~ "255.255.255.255" # => 0
re =~ "255.255.255.256" # => nil

Problem

I want to verify whether a given string is valid IPv4 address or not in ruby. I tried as follows but what is does is matches values greater than 255 as well. How to limit each block's range from 0-255 ``` str="255.255.255.256" if str=~ /\b[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b/ puts true else puts false end ```

Original source

Related problems