ScriptingTCL

Regular Expressions in TCL

Admin
December 9, 2020
2 min read

Regular expressions in TCL are a way to search text. These help us to find a particular character and then we can use our ways to deal with it. “regexp” represents the regular expression. This helps to match a character in TCL. Below are the few matching expressions.

#1. X -> Exact match.
#2. [a-z] -> Matches lower case letter from a-z.
#3. [A-Z] -> Matches upper case letter from A-Z.
#4. . (dot) -> Any character except newline or space.
#5. ^ -> Beginning of the string
#6. $ -> End of the string.
#7. [0-9] -> Matches the numeric character.
#8. \<special character> -> If looking to match a special character then use backslash.
#9. * -> matches globally
#10. + -> One or more character from search pattern.
#11. ? -> Optional search
#12. \w -> All words
#13. \s -> All backspace
#14. \w {4} -> Will search for only 4 digit word
#15. \w {4,} -> Will search for 4 and more digits.
#16. \w {4,6} -> Will search for words between 4 and 6.
#17. () -> Group of search pattern.
Eg. (t|T) -> t and T, it will search for both.

Syntax: regexp <character_to_be_searched> <search_from> variable_name

Eg.

% regexp i ivlsi var
1
% puts $var
i
% regexp t ivlsi var
0
% regexp -all i "ivlsi vlsi" var
3
% if { [regexp i ivlsi var] == 1} {
puts "$var"
}
I

Share the Article