Regexp in TCL
Regexp in TCL
Regexp in TCL
syntax:
[regexp "search_pattern $content]
example:1
example:2
List of all the metacharacters,quantifiers and specical sequences that can be used
in regular expression in TCL:
Meta characters
---------------
.(dot)-->matches any single character execpt newline.
set string "abcd"
set b [regexp "^a..d$" $string]
puts $b
#output is b=1
|(pipe)-->matches either the pattern on its left or the pattern on its right
Quantifiers
-----------
set string "The quick brown fox jump over the lazy dog"
set b [regexp "o*" $string]
puts $b
set string "The quick brown fox jump over the lazy dog"
if {[regexp "o+" $string]} {
puts "match found"
} else {
puts "no match found"
}
set string "The quick brown fox jump over the lazy dog"
set b [regexp "ou?" $string]
puts $b
set string "The quick brown fox jump over the lazy dog"
if {[regexp "ou?" $string]} {
puts "match found"
} else {
puts "no match found"
}
Example1:
set a {the mentor for funtional_verification2023 is : mohan_kumar_ns}
set b [regexp -nocase {the mentor for [a-z0-9_]+ is : [a-z]+} $a]
puts $b
example2:
example3:
lassign $argv
set file [open "text1.txt" r]
while {[gets $file line]>=0} {
if {$argv==[lindex $line 0]} {
set b [regexp -nocase {([a-z]+) :([-0-9]+)} $line match match2 match3]
}
}
puts "the value of $argv is : $match3"
example:4
1.read the VHDL.txt file
2. go through line by line.
3. write the regular expression to capture
all the entity names.
4.
5. write all the entity names in to new file(VHDL_enitity.txt)
set i 0
while {[gets $fh_read line] >= 0} {
incr i
if {[regexp -nocase { *entity +([a-z_]+) +is} $line match match1]} {
puts "the line number of the enity is:$i and entity name is:$match1"
puts $fh_write "the line number of the enity is:$i and entity name is:$match1"
}
}
example:5