Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Regexp in TCL

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

Regular expression in TCL:

it is used to match the pattern in the text.

In TCL, regular expression is pattern that describes a set of strings,


extract the information, and perfrom complex text processing tasks.

regexp command is supporting to match the pattern.

this command allows to perfrom various operations such as pattern matching


substitution and splitting on the strings using regular expression.

syntax:
[regexp "search_pattern $content]

example:1

set a "hello world"

if {[regexp "hello" $a]} {


puts "pattern matchs"
} else {
puts "pattern not matchs"
}

example:2

set a "HELLO world"

if {[regexp -nocase "hello" $a]} {


puts "pattern matchs"
} else {
puts "pattern not matchs"
}

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

set string "I have dog"


if {[regexp "h.e" $string]} {
puts "Match found"
} else {
puts "No Match found"
}
#output is No Match found

|(pipe)-->matches either the pattern on its left or the pattern on its right

set string "I have a dog"


set b [regexp "cat|dog" $string]
puts $b
[...](bracket expression)-->matches any single charcter from the specified set of
characters
[^...](negated bracket expression)-->matches any single character not in the
specified set of characters
()grouping --> groups multiple pattern together.
\(escape)-->escapes a meta character,allowing it to be matched as aliteral
character.
{...}(curly braces)-->matches the enclosed pattern exactly.
example:[a-z]{3}

Quantifiers
-----------

*(asterisk)-->matches zero or more occurrances of the preceding character or


subexpression.
ex1:
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 "o*" $string]
puts $b

+(plus)-->matches one or more occurances of the preceding character or


subexpression
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"
}

?(question mark)-->matches zero or one occurances of the preceding character or


subexpression.

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"
}

{n}(exact quantifiers)-->matches exactly "n" occurances of the preceding characters


or sub expressions
{n,}(minimum quantifier)-->matches at least n occurance of the preceding character
or sub expressions

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

set a {the mentor for physicaldesign_2023 is : chethan_kumar_ns}


set b [regexp -nocase {the mentor for ([a-z0-9_]+) is * :* ([a-z_]+)} $a match1
match2 match3]
puts $b
puts "this will print the whole expression :$match1"
puts "this will print the course_name:$match2"
puts "this will print the mentor_name:$match3"

example2:

set a {sram :-2500}


set b [regexp -nocase {([a-z]+) :([-0-9]+)} $a m1 m2 m3]
puts $b
puts "the cell_name is:$m2"
puts " the cell_value is:$m3

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"

x --> Exact match

[a-z] --> any lowercase from a to z.

[A-Z] --> any uppercase from A to Z.

. --> any character

^ --> Begining string should match

$ --> Ending string should match

() --> Add the sequence inside parenthesis to make a RE.

x* --> should match 0 or more occurences of the preceding x

x+ --> should match 1 or more ocuurences of the preceding x

\^ --> backlash sequnece to match special charater.

[a-z]* --> should match 0 or more occurence of the preceding x


{digit} --> matchs exactly digit ocuurences.
digit occureneces 0 to 9.

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 fh_read [open "VHDL.txt" r]


set fh_write [open "VHDL_entity.txt" w]

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

1. Read the VHDL.txt file


2. Read line by line
3. extract all the port name, port direction, port width
4. write the content in to VHDL_port.txt

set fh_read [open "VHDL.txt" r]


set fh_write [open "VHDL_port_details.txt" w]

while {[gets $fh_read line] >= 0 } {

if {[regexp -nocase { *([a-z0-9_, ]*) *: *(in|out) +std_logic_Vector\(([0-9]+)


+downto +([0-9]+)\)} $line match portname portdirection pwu pwl]} {
puts "port name is:$portname\tport direction is:$portdirection\tport width is:[expr
($pwu - $pwl) + 1]"
puts $fh_write "port name is:$portname\tport direction is:$portdirection\tport
width is:[expr ($pwu - $pwl) + 1]"

} elseif {[regexp -nocase { *([a-z0-9_, ]*) *: *(in|out) +std_logic} $line match


portname portdirection]} {
puts "port name is:$portname\tport direction is:$portdirection\tport width is:1"
puts $fh_write "port name is:$portname\tport direction is:$portdirection\tport
width is:1"

You might also like