Tcl - Regular Expressions

The "regexp" command is used to match a regular expression in Tcl. A regular expression is a sequence of characters that contains a search pattern. It consists of multiple rules and the following table explains these rules and corresponding use.
x
Exact match.
[a-z]
Any lowercase letter from a-z.
.
Any character.
^
Beginning string should match.
$
Ending string should match.
\^
Backlash sequence to match special character ^.Similarly you can use for other characters.
()
Add the above sequences inside parenthesis to make a regular expression.
x*
Should match 0 or more occurrences of the preceding x.
x+
Should match 1 or more occurrences of the preceding x.
[a-z]?
Should match 0 or 1 occurrence of the preceding x.
{digit}
Matches exactly digit occurrences of previous regex expression. Digit that contains 0-9.
{digit,}
Matches 3 or more digit occurrences of previous regex expression. Digit that contains 0-9.
{digit1,digit2}
Occurrences matches the range between digit1 and digit2 occurrences of previous regex expression.
The syntax for regex is given below −
regexp optionalSwitches patterns searchString fullMatch subMatch1 ... subMatchn

Here, regex is the command. We will see about optional switches later. Patterns are the rules as mentioned earlier. Search string is the actual string on which the regex is performed. Full match is any variable to hold the result of matched regex result. Submatch1 to SubMatchn are optional subMatch variable that holds the result of sub match patterns.
Let's look at some simple examples before diving into complex ones. A simple example for a string with any alphabets. When any other character is encountered the regex, search will be stopped and returned.
#!/usr/bin/tclsh

regexp {([A-Za-z]*)} "Tcl Tutorial" a b 
puts "Full Match: $a"
puts "Sub Match1: $b"
When the above code is executed, it produces the following result −
Full Match: Tcl
Sub Match1: Tcl

The following example shows how to search for multiple patterns. This is example pattern for any alphabets followed by any character followed by any alphabets.
#!/usr/bin/tclsh

regexp {([A-Za-z]*).([A-Za-z]*)} "Tcl Tutorial" a b c  
puts "Full Match: $a"
puts "Sub Match1: $b"
puts "Sub Match2: $c"
When the above code is executed, it produces the following result −
Full Match: Tcl Tutorial
Sub Match1: Tcl
Sub Match2: Tutorial

A modified version of the above code to show that a sub pattern can contain multiple patterns is shown below −
#!/usr/bin/tclsh

regexp {([A-Za-z]*.([A-Za-z]*))} "Tcl Tutorial" a b c  
puts "Full Match: $a"
puts "Sub Match1: $b"
puts "Sub Match2: $c"
When the above code is executed, it produces the following result −
Full Match: Tcl Tutorial
Sub Match1: Tcl Tutorial
Sub Match2: Tutorial

The list of switches available in Tcl are,
nocase − Used to ignore case.
nocase − Used to ignore case.
indices − Store location of matched sub patterns instead of matched characters.
indices − Store location of matched sub patterns instead of matched characters.
line − New line sensitive matching. Ignores the characters after newline.
line − New line sensitive matching. Ignores the characters after newline.
start index − Sets the offset of start of search pattern.
start index − Sets the offset of start of search pattern.
Marks the end of switches
Marks the end of switches
In the above examples, I have deliberately used [A-Z, a-z] for all alphabets, you can easily use -nocase instead of as shown below −
#!/usr/bin/tclsh

regexp -nocase {([A-Z]*.([A-Z]*))} "Tcl Tutorial" a b c  
puts "Full Match: $a"
puts "Sub Match1: $b"
puts "Sub Match2: $c"
When the above code is executed, it produces the following result −
Full Match: Tcl Tutorial
Sub Match1: Tcl Tutorial
Sub Match2: Tutorial

Another example using switches is shown below −
#!/usr/bin/tclsh

regexp -nocase -line -- {([A-Z]*.([A-Z]*))} "Tcl \nTutorial" a b 
puts "Full Match: $a"
puts "Sub Match1: $b"
regexp -nocase -start 4 -line -- {([A-Z]*.([A-Z]*))} "Tcl \nTutorial" a b  
puts "Full Match: $a"
puts "Sub Match1: $b"
When the above code is executed, it produces the following result −
Full Match: Tcl 
Sub Match1: Tcl 
Full Match: Tutorial
Sub Match1: Tutorial

 Print
 Add Notes
 Bookmark this page