Friday, April 17, 2015

Regular Expression for matching an ip address

What is ip address ?

=>An IP address is an identifier for a computer or device on a TCP/IP network. Networks using the TCP/IP protocol route messages based on the IP address of the destination.The format of an IP address is a 32-bit numeric address written as four numbers separated by periods. Each number can be zero to 255. For example, 1.160.10.240 could be an IP address.

Regular Expression (explained in TCL)

Program :

set a "192.168.10.25"

if {[regexp {^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$} $a]} {

puts "its an ip address"

}

Explanation :

[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5] : it matches any value from 0 - 255

^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.) : it matches the first byte i.e 192.

^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3} : it matches 192.168.10.



No comments:

Post a Comment