top of page

Find string length using regexp

 We can find the length of the string using regexp command, '.' is to map any charecter in given pattern,

 

set stiring "network"

set length [regexp -all . $string]

 

TCL program to Extract all IP address form the output given below.

OPSF:OSPF_NBRUP OSPF neighbor 216.138.181.15 (realm ospf-v2 e1-0/0/0:37.0 area 0.0.0.0) state changed from Full to Down due to KillNbr 13.12.1.0

 

set string "OSPF_NBRUP OSPF neighbor 216.138.181.15 (realm ospf-v2 e1-0/0/0:37.0 area 0.0.0.0) state changed from Full to Down due to KillNbr 13.12.1.0"

set a [regexp -all -inline {(?:\d+\.){3}\d+} $string]

puts $a

 

TCL program to List all words (consisting of all sequences of non-whitespace characters) in a string

regexp -all -inline {\S+} $string

 

TCL program to Reverse string

 

proc strrev str {

set res {}

set i [string length $str]

while {$i > 0} {

append res [string index $str [incr i -1]]}

set res

}


#matching any ip address
set str 192.168.3.110
regexp {(^[0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$} $str all first second third fourth
puts "$all \n $first \n $second \n $third \n $fourth \n"
if {$first <= 255 && $second <= 255 && $third <= 255 && $fourth <= 255} {
puts "valid ip address"
} else {
puts "invlaid ip address"
}


#to match any email address
set a "Raj_btech23@rediffmail.com"
if {[regexp -- {^[A-Za-z0-9._-]+@[[A-Za-z0-9.-]+$} $a b]} {
puts $b
} else {
puts fail
}
#puts $a


 

If the initial arguments to [regexp]] start with - then they are treated as switches. The following switches are currently supported:

-about

Instead of attempting to match the regular expression, returns a list containing information about the regular expression. The first element of the list is a subexpression count. The second element is a list of property names that describe various attributes of the regular expression. This switch is primarily intended for debugging purposes (see REGEXP DESCRIPTIVE FLAGS below).

-expanded

Enables use of the expanded regular expression syntax where whitespace and comments are ignored. This is the same as specifying the (?x) embedded option (see METASYNTAX, below).

-indices

Changes what is stored in the subMatchVars. Instead of storing the matching characters from string, each variable will contain a list of two integers giving the indices in string of the first and last characters in the matching range of characters.

-line

Enables newline-sensitive matching. By default, newline is a completely ordinary character with no special meaning. With this flag, ‘[^’ bracket expressions and ‘.’ never match newline, ‘^’ matches an empty string after any newline in addition to its normal function, and ‘$’ matches an empty string before any newline in addition to its normal function. This flag is equivalent to specifying both -linestop and -lineanchor, or the (?n) embedded option (see METASYNTAX, below).

-linestop

Changes the behavior of ‘[^’ bracket expressions and ‘.’ so that they stop at newlines. This is the same as specifying the (?p) embedded option (see METASYNTAX, below).

-lineanchor

Changes the behavior of ‘^’ and ‘$’ (the “anchors”) so they match the beginning and end of a line respectively. This is the same as specifying the (?w) embedded option (see METASYNTAX, below).

-nocase

Causes upper-case characters in string to be treated as lower case during the matching process.

-start index

Specifies a character index offset into the string to start matching the regular expression at. When using this switch, ‘^’ will not match the beginning of the line, and \A will still match the start of the string at index. If -indices is specified, the indices will be indexed starting from the absolute beginning of the input string. index will be constrained to the bounds of the input string.

-- Marks the end of switches. The argument following this one will be treated as exp even if it starts with a -.

Regular Expressions

© 2015 Thirumurugan

bottom of page