TCL String Manipulation and Pattern Matching

TCL String Manipulation and Pattern Matching

1. Format and Scan Functions

The format and scan functions in TCL serve similar purposes to their counterparts in C programming. The format function combines different data types into a single string, while scan extracts data from a formatted string.

set userName Sarah
set userAge 25
set userInfo [format "Name: %s, Age: %d" $userName $userAge]
puts $userInfo
scan $userInfo "Name: %s, Age: %d" firstName userAge
puts $firstName\ and\ $userAge

This demonstrates the basic process of combining variables into a string and then extracting them back out.

Name: Sarah, Age: 25
Sarah and 25

2. Regular Expression Matching

The regexp command matches strings against regular expression patterns. It returns 1 if the pattern fully matches, otherwise 0. Additional parameters can capture subexpressions from the match.

puts [regexp {^((0x)?[0-9a-fA-F]+|[0-9]+)$} cd]

# Pattern explanation:
# |       - Either the left or right pattern must match
# (0x)?   - Optional '0x' prefix
# [0-9a-fA-F] - Hexadecimal digits (case insensitive)
# +       - One or more repetitions
# ^       - Match from the beginning
# $       - Match until the end

regexp { ([0-9]+) *([a-z]+)} "There are 500 oranges" total quantity fruit
puts $total\n$quantity\n$fruit
# First variable: entire match
# Second variable: first subexpression (numbers)
# Third variable: second subexpression (letters)
# Subexpressions are defined by parentheses

regexp {^((0x)?[0-9a-fA-F]+|[0-9]+)$} 0x1a2b3c S1 S2 S3
puts $S1\n$S2\n$S3
1
 500 oranges
500
oranges
0x1a2b3c
0x1a2b3c
0x

The first example successfully matches hexadecimal or decimal numbers.

The second example matches numbers followed by letters. The first subexpression captures the numeric part, while the second captures the alphabetic part.

The third example matches hexadecimal numbers. The first variable captures the entire match, the second captures the full hexadecimal number, and the third captures the '0x' prefix if present.

The number of parameters passed to regexp after the pattern is determined by the number of parentheses in the expression. For {()()}, the parameters correspond to the entire match, first subexpression, and second subexpression. For {(())}, they correspond to the entire match, outer subexpression, and inner subexpression.

Tags: TCL String Manipulation regular expressions format scan

Posted on Fri, 08 May 2026 01:48:36 +0000 by cpharry