Tcl regexp Command
last modified October 20, 2026
The Tcl regexp command matches regular expressions against strings.
It's a powerful tool for pattern matching and text extraction. The command
returns 1 if the pattern matches, 0 otherwise (with -inline it
returns the matched strings instead, and with -all the number of
matches). Tcl uses its own flavor of regular expressions, called advanced
regular expressions (AREs).
Basic Definition
Regular expressions are patterns used to match character combinations in strings.
The regexp command follows this syntax: regexp ?switches?
exp string ?matchVar? ?subMatchVar ...?.
The exp is the regular expression pattern. The string
is the input text to match against. Optional match variables store the matched
text: the first variable receives the whole match, and each following variable
receives one parenthesized subpattern.
Simple Pattern Matching
This example demonstrates basic pattern matching with regexp.
We check if a string contains digits.
set text "Order 12345 was placed on 2023-05-15"
if {[regexp {\d+} $text]} {
puts "Digits found in text"
} else {
puts "No digits found"
}
The pattern \d+ matches one or more digits. The command returns 1
if any digits are found. The match result is used in a conditional statement.
Extracting Matches
This example shows how to extract matched substrings using match variables.
set email "john.doe@example.com"
regexp {([^@]+)@(.+)} $email full user domain
puts "Full: $full"
puts "User: $user"
puts "Domain: $domain"
The pattern captures the username and domain parts of an email address.
The first capture group ([^@]+) matches everything before @.
The second (.+) matches everything after. The variables store
the matches.
Case-Insensitive Matching
The -nocase switch enables case-insensitive pattern matching.
set text "The Quick Brown Fox"
if {[regexp -nocase {quick} $text]} {
puts "Found 'quick' (case insensitive)"
}
Without -nocase, the pattern would only match lowercase 'quick'.
The switch makes the match case-insensitive, so it matches 'Quick' in the text.
Anchored Matching
Anchors ensure the pattern matches at specific positions in the string.
set names [list "John Smith" "Alice Johnson" "Smith Brown"]
foreach name $names {
if {[regexp {^Smith} $name]} {
puts "$name starts with Smith"
} elseif {[regexp {Smith$} $name]} {
puts "$name ends with Smith"
}
}
^ anchors the match to the start of the string, while $
anchors to the end. The script checks if 'Smith' appears at the start or end
of each name.
Advanced Subpattern Extraction
This example demonstrates extracting multiple subpatterns from a complex string.
set log {ERROR 2023-05-15 14:30:45 [MainThread] Connection timeout}
regexp {(\w+)\s+(\d{4}-\d{2}-\d{2})\s+(\d{2}:\d{2}:\d{2})\s+\[(\w+)\]\s+(.*)} \
$log all level date time thread message
puts "Level: $level"
puts "Date: $date"
puts "Time: $time"
puts "Thread: $thread"
puts "Message: $message"
The pattern extracts five components from a log entry: error level, date, time,
thread name, and message. Each component is captured in parentheses and stored
in a separate variable. The first variable, all, receives the whole
match, so it must be listed before the five capture variables; leaving it out
would shift every value by one position. The brackets around the thread name
are escaped in the pattern because they are literal characters here.
Using Switches
This example combines multiple switches for more powerful matching.
set text "First line\nSecond line\nThird line"
set matches [regexp -all -inline -lineanchor {^\w+ line$} $text]
puts "Matches: $matches"
The -all switch finds all matches, -inline returns
them as a list, and -lineanchor makes ^ and $ match at line
boundaries. Without -lineanchor, ^ and $ match only at the start
and end of the whole string, so the pattern would find nothing here. The
command returns the three lines, each of which is a word followed by "line".
Best Practices
- Readability: Use comments for complex patterns.
- Braces: Put patterns in braces so that Tcl does not process backslashes and brackets before
regexpsees them. - Escaping: Escape characters that are special in regular expressions, such as
.and[, when you mean them literally. - Simple tests: For a fixed substring or a glob pattern,
string firstandstring matchare simpler thanregexp. - Performance: Tcl caches compiled patterns automatically, so reuse the same pattern text instead of building a new string each time.
This tutorial covered the Tcl regexp command with practical
examples showing pattern matching and text extraction techniques.
Author
List all Tcl Tutorials.