Performing character substitution on a string

If regexp is a Find function, then regsub is equivalent to Find and Replace. The regsub command accepts a string and using Regular Expression pattern matching, it locates and, if desired, replaces the pattern with the desired value. The syntax of regsub is similar to regexp as are the switches. However, additional control over the substitution is added. The switches are as listed next:

Switch

Description

-all

Causes the command to perform substitution for each match found

The & and sequences are handled for each substitution

-expanded

Allows use of expanded regular expression wherein whitespace and comments are ignored

-line

Enables newline-sensitive matching similar to passing the linestop and lineanchor switches

-linestop

Changes the behavior of [^] bracket expressions so that they stop at newline characters

-lineanchor

Changes the behavior of ^ and $ (anchors) so that they match both the beginning and end of a line

-nocase

Treats uppercase characters in the search string as lowercase

-start

Allows specification of a character offset in the string from which to start matching

Now that we have a background in switches as they apply to the regsub command, let's look at the command:

	regsub switches expression string substitution variable

The regsub command matches the expression against the string provided and either copies the string to the variable or returns the string if a variable is not provided. If a match is located, the portion of the string that matched is replaced by substitution. Whenever a substitution contains an& or a character, it is replaced with the portion of the string that matches the expression. If the substitution contains the switch " " (where n represents a numeric value between 1 and 9), it is replaced with the portion of the string that matches with the nth sub-expression of the expression. Additional backslashes may be used in the substitution to prevent interpretation of the&, , , and the backslashes themselves. As both the regsub command and the Tcl interpreter perform backslash substitution, you should enclose the string in curly braces to prevent unintended substitution.

How to do it…

In the following example, we will substitute every instance of the word one, which is a word by itself, with the word three. Return values from the commands are provided for clarity. Enter the following command:


% set original "one two one two one two"
one two one two one two

% regsub -all {one} $original three new
3

% puts $new
three two three two three two

How it works…

As you can see, the value returned from the regsub command lists the number of matches found. The string original has been copied into the string new, with the substitutions completed. With the addition of additional switches, you can easily parse a lengthy string variable and perform bulk updates. I have used this to rapidly parse a large text file prior to importing data into a database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.166.255