regsub − Perform substitutions based on regular expression pattern matching


regsub ?switches? exp string subSpec ?varName? ___________________________


This command matches the regular expression exp against string, and either copies string to the variable whose name is given by varName or returns string if varName is not present. (Regular expression matching is described in the re_syntax reference page.) If there is a match, then while copying string to varName (or to the result of this command if varName is not present) the portion of string that matched exp is replaced with subSpec. If subSpec contains a “&” or “\0”, then it is replaced in the substitution with the portion of string that matched exp. If subSpec contains a “\n”, where n is a digit between 1 and 9, then it is replaced in the substitution with the portion of string that matched the n’th parenthesized subexpression of exp. Additional backslashes may be used in subSpec to prevent special interpretation of “&”, “\0”, “\n” and backslashes. The use of backslashes in subSpec tends to interact badly with the Tcl parser’s use of backslashes, so it is generally safest to enclose subSpec in braces if it includes backslashes.

If the initial arguments to regsub start with then they are treated as switches. The following switches are currently supported:


All ranges in string that match exp are found and substitution is performed for each of these ranges. Without this switch only the first matching range is found and substituted. If −all is specified, then “&” and “\n” sequences are handled for each substitution using the information from the corresponding match.


Enables use of the expanded regular expression syntax where whitespace and comments are ignored. This is the same as specifying the (?x) embedded option (see the re_syntax manual page).


Enables newline-sensitive matching. By default, newline is a completely ordinary character with no special meaning. With this flag, “[^” bracket expressions and “.” never match newline, “^” matches an empty string after any newline in addition to its normal function, and “$” matches an empty string before any newline in addition to its normal function. This flag is equivalent to specifying both −linestop and −lineanchor, or the (?n) embedded option (see the re_syntax manual page).


Changes the behavior of “[^” bracket expressions and “.” so that they stop at newlines. This is the same as specifying the (?p) embedded option (see the re_syntax manual page).


Changes the behavior of “^” and “$” (the “anchors”) so they match the beginning and end of a line respectively. This is the same as specifying the (?w) embedded option (see the re_syntax manual page).


Upper-case characters in string will be converted to lower-case before matching against exp; however, substitutions specified by subSpec use the original unconverted form of string.

−start index

Specifies a character index offset into the string to start matching the regular expression at. The index value is interpreted in the same manner as the index argument to string index. When using this switch, “^” will not match the beginning of the line, and \A will still match the start of the string at index. index will be constrained to the bounds of the input string.


Marks the end of switches. The argument following this one will be treated as exp even if it starts with a .

If varName is supplied, the command returns a count of the number of matching ranges that were found and replaced, otherwise the string after replacement is returned. See the manual entry for regexp for details on the interpretation of regular expressions.


Replace (in the string in variable string) every instance of foo which is a word by itself with bar:

regsub -all {\mfoo\M} $string bar string

or (using the “basic regular expression” syntax):

regsub -all {(?b)\<foo\>} $string bar string

Insert double-quotes around the first instance of the word interesting, however it is capitalized.

regsub -nocase {\yinteresting\y} $string {"&"} string

Convert all non-ASCII and Tcl-significant characters into \u escape sequences by using regsub and subst in combination:

# This RE is just a character class for almost everything "bad"
set RE {[][{};#\\\$ \r\t\u0080-\uffff]}

# We will substitute with a fragment of Tcl script in brackets
set substitution {[format \\\\u%04x [scan "\\&" %c]]}

# Now we apply the substitution to get a subst-string that
# will perform the computational parts of the conversion. Note
# that newline is handled specially through string map since
# backslash-newline is a special sequence.
set quoted [subst [string map {\n {\\u000a}} \
[regsub -all $RE $string $substitution]]]


regexp(n), re_syntax(n), subst(n), string(3)


match, pattern, quoting, regular expression, substitution


Personal Opportunity - Free software gives you access to billions of dollars of software at no cost. Use this software for your business, personal use or to develop a profitable skill. Access to source code provides access to a level of capabilities/information that companies protect though copyrights. Open source is a core component of the Internet and it is available to you. Leverage the billions of dollars in resources and capabilities to build a career, establish a business or change the world. The potential is endless for those who understand the opportunity.

Business Opportunity - Goldman Sachs, IBM and countless large corporations are leveraging open source to reduce costs, develop products and increase their bottom lines. Learn what these companies know about open source and how open source can give you the advantage.

Free Software

Free Software provides computer programs and capabilities at no cost but more importantly, it provides the freedom to run, edit, contribute to, and share the software. The importance of free software is a matter of access, not price. Software at no cost is a benefit but ownership rights to the software and source code is far more significant.

Free Office Software - The Libre Office suite provides top desktop productivity tools for free. This includes, a word processor, spreadsheet, presentation engine, drawing and flowcharting, database and math applications. Libre Office is available for Linux or Windows.

Free Books

The Free Books Library is a collection of thousands of the most popular public domain books in an online readable format. The collection includes great classical literature and more recent works where the U.S. copyright has expired. These books are yours to read and use without restrictions.

Source Code - Want to change a program or know how it works? Open Source provides the source code for its programs so that anyone can use, modify or learn how to write those programs themselves. Visit the GNU source code repositories to download the source.


Study at Harvard, Stanford or MIT - Open edX provides free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses for almost all major subjects and course levels. Open edx also offers some paid courses and selected certifications.

Linux Manual Pages - A man or manual page is a form of software documentation found on Linux/Unix operating systems. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts.