Skip to main content

Regular Expressions in TCL regexp and regsub

           Regular expressions are most widely used in the scripting languages to get the desired search pattern.

If you want to search for a particular pattern in a file, it becomes difficult to search manually if the file size is big. In such cases it is good to learn any of the scripting language which does your job.

Here, we will discuss the scripting language TCL which makes use of regular expression to search for your pattern in a file.

In TCL all you have to do is to write a small TCL script which does your work.

For ex:
set f1 [open "file.txt"]
set line [split [read $f] "\n"]
foreach e $line {
   if { [regexp {mountain} $e] } {
             puts "Found!"
close $f1
The above example makes use of regular expression regexp in if block of the script to search for the word mountain in every line of the file. Lets see how it works:
regexp {mountain} $e
Lines in the files are considered as a string during search. It can be stored in the variable or it can be used within " " ( double quotes ) in regexp command. Similarly the search pattern can be stored in variable or it can be written in {} ( Curly braces ). The output of the regexp command will be 1, if search pattern is found else it will be 0
regexp -inline {mountain} $e
When the switch -inline is used, the output will be the matched pattern but not 1 or 0.
regexp {mountain} $e mp
In this case the matched pattern will be assigned to the variable mp. 
regexp -all -inline {mountain} $e
The output contains the last matched pattern
regexp {^mountain} $e
^ called anchor, is used to search for pattern at beginning of the string. 
regexp {mountain$} $e
$ called dollar, is used to search for pattern at end of the string.
regexp {^mountain$} $e
To search for exact pattern in the string. If string contains any other characters, the output will be 0.
regexp {[a-z]+} $e
[] are used to specify range in search pattern. + symbol indicates the range specified in [] to occur 1 or more times. Note that only small alphabets are specified in the range.
regexp {[a-zA-Z]+} $e
Both capital and small alphabets are taken care by search pattern
regexp {[0-9]+} $e
Numbers will be searched
regexp {[a-zA-Z0-9]+} $e
All alphanumeric patterns are searched.
regexp {\d+} $e
It searches for digits.
regexp {\D+} $e
It searches for not of digits.
regexp {\s+} $e
It searches for spaces
regexp {\S+} $e 
It searches for not of spaces
regexp {\d{1,3}} $e
It searches for digits where {1,3} specifies minimum of 1 digit and maximum of 3 digits. {min,max} used to specify the min. and max. values for the search pattern previous to it.
regexp {[a-zA-Z]\d{2,4}} $e
Note here that range {2,4} is only for digits and not for the alphabets. It searches for any case insensitive alphabets followed by two or three or four digits.

Popular posts from this blog

eLitmus pH test for freshers

eLitmus has became more famous in conducting pH test for the fresh graduates in India. They will evaluate candidates across India and to provide a standard benchmark against which companies can compare them.

Who can apply

Candidates having/pursuing a degree in BE/B.Tech, MCA, ME/M.Tech or M.Sc(CS/IT) can apply for the pH test.

Companies recruiting from pH test

List of companies

Look at test centers

Check the upcoming pH tests

Look at the testimonials

Read what the candidates appeared for this exam says about eLitmus.

Test Pattern

Test duration : 2 hours
No of questions : 60

It will be asked from 3 categories,


20 : Problem solving
20 : Verbal ( You need to read the paragraph and answer the questions )
20 : Quantitative ( Data interpretation )

Four options will be provided for each question. Test will be conducted online.

Payment methods

Fresh graduates appearing for the test has to pay Rs 920/- as a test fee.

They can pay online or send a DD or in cash at Bangalore eLitmus office.

By Online: If y…