regex - IPV6 address validation using shell script -


i using below script validate ipv6 address script pass invalid ip such empty string, name etc.

if [[ "$ipv6addr"=~"^:?([a-fa-f0-9]{1,4}(:|.)?){0,8}(:|::)?([a-fa-f0-9]{1,4}(:|.)?){0,8}$" ]];   echo "valid ipv6 addr" else   echo "invalid ip address" fi 

can identify what's wrong in regex, please?

the reason why passes empty string of content optional or allowed match 0 times:

  • ^ - match beginning of string
  • :? - optional colon
  • ([a-fa-f0-9]{1,4}(:|.)?){0,8} - matches 0 - 8 times (thus optional)
  • (:|::)? - optional colon or double colon
  • ([a-fa-f0-9]{1,4}(:|.)?){0,8} - matches 0 - 8 times (thus optional)
  • $ - matches end of string

thus blank string allowed, pattern allows string not match of optional parts.


looking @ rfc 4291, defines ip 6 specification, section 2.2 defines 3 methods of representing address. may easiest, if need match forms define them separately , combine separate regex's together, in

^(regex_pattern1|regex_pattern2|regex_pattern3)$ 

where pattern1, example, (?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4} , patterns 2 , 3 other cases (pattern 1 taken regular expressions cookbook, 2nd edition (o'reilly, 2012))

or, better (for readability), test them in serial (pseudocode follows),

if (matches pattern 1)     echo 'valid' else if (matches pattern 2)     echo 'valid' else if (matches pattern 3)     echo 'valid' else     echo 'invalid' 

see, also, question more information.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -