| 1 | '\" | 
|---|
| 2 | '\" Copyright (c) 1993 The Regents of the University of California. | 
|---|
| 3 | '\" Copyright (c) 1994-1996 Sun Microsystems, Inc. | 
|---|
| 4 | '\" Copyright (c) 2000 Scriptics Corporation. | 
|---|
| 5 | '\" | 
|---|
| 6 | '\" See the file "license.terms" for information on usage and redistribution | 
|---|
| 7 | '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. | 
|---|
| 8 | '\"  | 
|---|
| 9 | '\" RCS: @(#) $Id: regsub.n,v 1.22 2007/12/13 15:22:33 dgp Exp $ | 
|---|
| 10 | '\"  | 
|---|
| 11 | .so man.macros | 
|---|
| 12 | .TH regsub n 8.3 Tcl "Tcl Built-In Commands" | 
|---|
| 13 | .BS | 
|---|
| 14 | '\" Note:  do not modify the .SH NAME line immediately below! | 
|---|
| 15 | .SH NAME | 
|---|
| 16 | regsub \- Perform substitutions based on regular expression pattern matching | 
|---|
| 17 | .SH SYNOPSIS | 
|---|
| 18 | \fBregsub \fR?\fIswitches\fR? \fIexp string subSpec \fR?\fIvarName\fR? | 
|---|
| 19 | .BE | 
|---|
| 20 | .SH DESCRIPTION | 
|---|
| 21 | .PP | 
|---|
| 22 | This command matches the regular expression \fIexp\fR against | 
|---|
| 23 | \fIstring\fR, | 
|---|
| 24 | and either copies \fIstring\fR to the variable whose name is | 
|---|
| 25 | given by \fIvarName\fR or returns \fIstring\fR if \fIvarName\fR is not | 
|---|
| 26 | present. | 
|---|
| 27 | (Regular expression matching is described in the \fBre_syntax\fR | 
|---|
| 28 | reference page.) | 
|---|
| 29 | If there is a match, then while copying \fIstring\fR to \fIvarName\fR | 
|---|
| 30 | (or to the result of this command if \fIvarName\fR is not present) | 
|---|
| 31 | the portion of \fIstring\fR that | 
|---|
| 32 | matched \fIexp\fR is replaced with \fIsubSpec\fR. | 
|---|
| 33 | If \fIsubSpec\fR contains a | 
|---|
| 34 | .QW & | 
|---|
| 35 | or | 
|---|
| 36 | .QW \e0 , | 
|---|
| 37 | then it is replaced in the substitution with the portion of | 
|---|
| 38 | \fIstring\fR that matched \fIexp\fR. | 
|---|
| 39 | If \fIsubSpec\fR contains a | 
|---|
| 40 | .QW \e\fIn\fR , | 
|---|
| 41 | where \fIn\fR is a digit | 
|---|
| 42 | between 1 and 9, then it is replaced in the substitution with | 
|---|
| 43 | the portion of \fIstring\fR that matched the \fIn\fR'th | 
|---|
| 44 | parenthesized subexpression of \fIexp\fR. | 
|---|
| 45 | Additional backslashes may be used in \fIsubSpec\fR to prevent special | 
|---|
| 46 | interpretation of | 
|---|
| 47 | .QW & , | 
|---|
| 48 | .QW \e0 , | 
|---|
| 49 | .QW \e\fIn\fR | 
|---|
| 50 | and backslashes. | 
|---|
| 51 | The use of backslashes in \fIsubSpec\fR tends to interact badly | 
|---|
| 52 | with the Tcl parser's use of backslashes, so it is generally | 
|---|
| 53 | safest to enclose \fIsubSpec\fR in braces if it includes | 
|---|
| 54 | backslashes. | 
|---|
| 55 | .LP | 
|---|
| 56 | If the initial arguments to \fBregsub\fR start with \fB\-\fR then | 
|---|
| 57 | they are treated as switches.  The following switches are | 
|---|
| 58 | currently supported: | 
|---|
| 59 | .TP 10 | 
|---|
| 60 | \fB\-all\fR | 
|---|
| 61 | All ranges in \fIstring\fR that match \fIexp\fR are found and | 
|---|
| 62 | substitution is performed for each of these ranges. | 
|---|
| 63 | Without this switch only the first | 
|---|
| 64 | matching range is found and substituted. | 
|---|
| 65 | If \fB\-all\fR is specified, then | 
|---|
| 66 | .QW & | 
|---|
| 67 | and | 
|---|
| 68 | .QW \e\fIn\fR | 
|---|
| 69 | sequences are handled for each substitution using the information | 
|---|
| 70 | from the corresponding match. | 
|---|
| 71 | .TP 15 | 
|---|
| 72 | \fB\-expanded\fR | 
|---|
| 73 | Enables use of the expanded regular expression syntax where | 
|---|
| 74 | whitespace and comments are ignored.  This is the same as specifying | 
|---|
| 75 | the \fB(?x)\fR embedded option (see the \fBre_syntax\fR manual page). | 
|---|
| 76 | .TP 15 | 
|---|
| 77 | \fB\-line\fR | 
|---|
| 78 | Enables newline-sensitive matching.  By default, newline is a | 
|---|
| 79 | completely ordinary character with no special meaning.  With this flag, | 
|---|
| 80 | .QW [^ | 
|---|
| 81 | bracket expressions and | 
|---|
| 82 | .QW . | 
|---|
| 83 | never match newline, | 
|---|
| 84 | .QW ^ | 
|---|
| 85 | matches an empty string after any newline in addition to its normal | 
|---|
| 86 | function, and | 
|---|
| 87 | .QW $ | 
|---|
| 88 | matches an empty string before any newline in | 
|---|
| 89 | addition to its normal function.  This flag is equivalent to | 
|---|
| 90 | specifying both \fB\-linestop\fR and \fB\-lineanchor\fR, or the | 
|---|
| 91 | \fB(?n)\fR embedded option (see the \fBre_syntax\fR manual page). | 
|---|
| 92 | .TP 15 | 
|---|
| 93 | \fB\-linestop\fR | 
|---|
| 94 | Changes the behavior of | 
|---|
| 95 | .QW [^ | 
|---|
| 96 | bracket expressions and | 
|---|
| 97 | .QW . | 
|---|
| 98 | so that they | 
|---|
| 99 | stop at newlines.  This is the same as specifying the \fB(?p)\fR | 
|---|
| 100 | embedded option (see the \fBre_syntax\fR manual page). | 
|---|
| 101 | .TP 15 | 
|---|
| 102 | \fB\-lineanchor\fR | 
|---|
| 103 | Changes the behavior of | 
|---|
| 104 | .QW ^ | 
|---|
| 105 | and | 
|---|
| 106 | .QW $ | 
|---|
| 107 | (the | 
|---|
| 108 | .QW anchors ) | 
|---|
| 109 | so they match the | 
|---|
| 110 | beginning and end of a line respectively.  This is the same as | 
|---|
| 111 | specifying the \fB(?w)\fR embedded option (see the \fBre_syntax\fR | 
|---|
| 112 | manual page). | 
|---|
| 113 | .TP 10 | 
|---|
| 114 | \fB\-nocase\fR | 
|---|
| 115 | Upper-case characters in \fIstring\fR will be converted to lower-case | 
|---|
| 116 | before matching against \fIexp\fR;  however, substitutions specified | 
|---|
| 117 | by \fIsubSpec\fR use the original unconverted form of \fIstring\fR. | 
|---|
| 118 | .TP 10 | 
|---|
| 119 | \fB\-start\fR \fIindex\fR | 
|---|
| 120 | Specifies a character index offset into the string to start | 
|---|
| 121 | matching the regular expression at.   | 
|---|
| 122 | .VS 8.5 | 
|---|
| 123 | The \fIindex\fR value is interpreted in the same manner | 
|---|
| 124 | as the \fIindex\fR argument to \fBstring index\fR. | 
|---|
| 125 | .VE 8.5 | 
|---|
| 126 | When using this switch, | 
|---|
| 127 | .QW ^ | 
|---|
| 128 | will not match the beginning of the line, and \eA will still | 
|---|
| 129 | match the start of the string at \fIindex\fR. | 
|---|
| 130 | \fIindex\fR will be constrained to the bounds of the input string. | 
|---|
| 131 | .TP 10 | 
|---|
| 132 | \fB\-\|\-\fR | 
|---|
| 133 | Marks the end of switches.  The argument following this one will | 
|---|
| 134 | be treated as \fIexp\fR even if it starts with a \fB\-\fR. | 
|---|
| 135 | .PP | 
|---|
| 136 | If \fIvarName\fR is supplied, the command returns a count of the | 
|---|
| 137 | number of matching ranges that were found and replaced, otherwise the | 
|---|
| 138 | string after replacement is returned. | 
|---|
| 139 | See the manual entry for \fBregexp\fR for details on the interpretation | 
|---|
| 140 | of regular expressions. | 
|---|
| 141 | .SH EXAMPLES | 
|---|
| 142 | Replace (in the string in variable \fIstring\fR) every instance of | 
|---|
| 143 | \fBfoo\fR which is a word by itself with \fBbar\fR: | 
|---|
| 144 | .CS | 
|---|
| 145 | \fBregsub\fR -all {\e<foo\e>} $string bar string | 
|---|
| 146 | .CE | 
|---|
| 147 | .PP | 
|---|
| 148 | Insert double-quotes around the first instance of the word | 
|---|
| 149 | \fBinteresting\fR, however it is capitalized. | 
|---|
| 150 | .CS | 
|---|
| 151 | \fBregsub\fR -nocase {\e<interesting\e>} $string {"&"} string | 
|---|
| 152 | .CE | 
|---|
| 153 | .PP | 
|---|
| 154 | Convert all non-ASCII and Tcl-significant characters into \eu escape | 
|---|
| 155 | sequences by using \fBregsub\fR and \fBsubst\fR in combination: | 
|---|
| 156 | .CS | 
|---|
| 157 | # This RE is just a character class for everything "bad" | 
|---|
| 158 | set RE {[][{};#\e\e\e$\es\eu0100-\euffff]} | 
|---|
| 159 |  | 
|---|
| 160 | # We will substitute with a fragment of Tcl script in brackets | 
|---|
| 161 | set substitution {[format \e\e\e\eu%04x [scan "\e\e&" %c]]} | 
|---|
| 162 |  | 
|---|
| 163 | # Now we apply the substitution to get a subst-string that | 
|---|
| 164 | # will perform the computational parts of the conversion. | 
|---|
| 165 | set quoted [subst [\fBregsub\fR -all $RE $string $substitution]] | 
|---|
| 166 | .CE | 
|---|
| 167 | .SH "SEE ALSO" | 
|---|
| 168 | regexp(n), re_syntax(n), subst(n), | 
|---|
| 169 | .VS 8.5 | 
|---|
| 170 | string(n) | 
|---|
| 171 | .VE | 
|---|
| 172 | .SH KEYWORDS | 
|---|
| 173 | match, pattern, regular expression, substitute | 
|---|