⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 77.html

📁 Tcl 语言的入门级图书
💻 HTML
字号:
<HTML><TITLE>Regexp and Regsub: Search and Replace</TITLE><BODY BGCOLOR="#FFF0E0" VLINK="#0FBD0F" TEXT="#101000" LINK="#0F0FDD">
<A NAME="top"><H1>Search and Replace</H1></A>


<P> Tcl has one more command that deals with regular expressions.  It is
<TT><NAME=#Cregsub>regsub</A></TT> and its purpose is to do search and replace operations.
This command is invoked as follows:

<P><CENTER><TABLE BORDER><TR><TD><DL> 
<DT><STRONG><PRE>regsub <CITE>?SWITCHES? PATTERN STRING REPLACE_PATTERN VAR_NAME</CITE></PRE></STRONG><DD>
Finds the first occurrence of <CITE>PATTERN</CITE> in <CITE>STRING</CITE>, replaces it
in the manner determined by <CITE>REPLACE_PATTERN</CITE> and assigns the resulting
string to <CITE>VAR_NAME</CITE>. 

<P> There is an <TT>-all</TT> switch to force replacement of
all occurrences of <CITE>PATTERN</CITE>.  In any case, the return value is the 
number of replacements.

<P> If there is no occurrence of <CITE>PATTERN</CITE>, <CITE>VAR_NAME</CITE> gets an unchanged
copy of <CITE>STRING</CITE>.
</DL></TD></TR></TABLE></CENTER></P>

<P> <CITE>REPLACE_PATTERN</CITE> may be a simple string you want substituted someplace
inside <CITE>STRING</CITE>.  For example,

<PRE>
regsub -all dog $Script cat Script
</PRE>

This command replaces all instances of "dog" in <TT>Script</TT> with "cat"
and puts the resulting string back into <TT>Script.</TT>

<P> As with glob and regular-expression patterns, <CITE>REPLACE_PATTERN</CITE> may contain
special characters that alter its meaning.  This is a third kind of pattern
you must learn.  Happily, it is much easier than the other two.  There are
two special characters, <TT>&#38;</TT> and <TT>\</TT>.

<P> If <CITE>REPLACE_PATTERN</CITE> contains the special character <TT>&#38;</TT> then the <TT>&#38;</TT>
stands for the entire substring that was matched.  So,

<PRE>
regsub -all cat $Str (&#38;) Str
</PRE>

will put parentheses around all occurrences of "cat."  

<P> <P><A NAME="7.7a">
<STRONG>Exercise 7.7a</STRONG> </A><DL><DD>
  Write a <TT>regsub</TT> command line that doubles each
occurrence of the character "&#38;" in a string <TT>Str</TT>. <P>
<A HREF="7.9.html#Sol7.7a" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/7.9.html#Sol7.7a">Solution</A></DL>

<P> 
<P> More complicated substitutions are possible by identifying substrings that
match subpatterns of <CITE>PATTERN</CITE> and using those substrings to build the
replacement pattern.  Substrings are identified with parentheses in
<CITE>PATTERN</CITE> the same way they are for the <TT>regexp</TT> command.

<P>  Suppose we have a line with a date in the form MONTH/DAY/YEAR you want
to put it into the form YEAR-MONTH-DAY.  For example, you want
"06/23/96" to become "96-06-23."  I am simplifying this problem a little
by assuming that the month, the day, and the year all contain exactly two
digits.

<P>  Using the preassigned pattern, <TT>Digit_</TT>, a date has this pattern:

<PRE>
($Digit_+)/($Digit_+)/($Digit_+)
</PRE>

and the three sets of parentheses identify the numbers you need.

<P>  This use of <TT>regexp</TT> would extract the month, day, and year.

<PRE>
regexp "($Digit_+)/($Digit_+)/($Digit_+)" $Line \
       Junk Month Day Year
</PRE>

<P>  The replacement string you want is 

<PRE>
$Year-$Month-$Day
</PRE>

However, you cannot write it that way when using <TT>regsub.</TT>  You do not get to
name the variables that match the parentheses.  Instead, the substrings that
match subpatterns are represented in <CITE>REPLACE_PATTERN</CITE> with <TT>\1,</TT>
<TT>\2,</TT> and so on.  The subpattern represented inside the leftmost parentheses is
represented with <TT>\1</TT>, and so on to the right.

<P>  Here is the complete <TT>regsub</TT> command for the date transforming example.

<PRE>
regsub "($Digit_+)/($Digit_+)/($Digit_+)" $Line \
       \\3-\\1-\\2 Line
</PRE>

Note <CITE>REPlACE_PATTERN</CITE> is <CITE>not</CITE> a regular expression and I do not have
any style rules for writing it.  As shown here, <CITE>REPLACE_PATTERN</CITE> is
interpreted first by the Tcl interpreter and then by <TT>regsub</TT>.  The first
interpretation replaces each <TT>\\</TT> with <TT>\</TT>.  The second replaces each
<TT>\i</TT> with the corresponding matched substring.

<P>  Finally, you should know that <TT>regsub</TT> will do backslash quoting to
permit you to have things like <TT>&#38;</TT> and <TT>\</TT> in your patterns.  Also,
<TT>regsub</TT> will treat <TT>\0</TT> just like <TT>&#38;</TT>.

<P> <P><A NAME="7.7b">
<STRONG>Exercise 7.7b</STRONG> </A><DL><DD>
 Write a <TT>regsub</TT> command line to replace all occurrences
of "cat" that are words with "dog" in the string <TT>Str.</TT>  For this
exercise, "word" means a string of letters which is bounded on the left and
right by something that is not a letter.

<P>  Now adjust it so that "cats" is replaced with "dogs."
<P>
<A HREF="7.9.html#Sol7.7b" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/7.9.html#Sol7.7b">Solution</A></DL>


<!-- Linkbar -->
<P><CENTER><FONT SIZE=2><NOBR>
<STRONG>From</STRONG>
<A HREF="javascript:if(confirm('http://www.mapfree.com/sbf/tcl/book/home.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.mapfree.com/sbf/tcl/book/home.html'" tppabs="http://www.mapfree.com/sbf/tcl/book/home.html">Tcl/Tk For Programmers</A><WBR>
<STRONG>Previous</STRONG>
<A HREF="7.6.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/7.6.html">section</A><WBR>
<STRONG>Next</STRONG>
<A HREF="7.8.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/7.8.html">section</A><WBR>
<STRONG>All</STRONG>
<A HREF="7.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/7.html">sections</A><WBR>
<STRONG>Author</STRONG>
<A HREF="javascript:if(confirm('http://www.mapfree.com/mp/jaz/home.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.mapfree.com/mp/jaz/home.html'" tppabs="http://www.mapfree.com/mp/jaz/home.html">J. A. Zimmer</A><WBR>
<STRONG>Copyright</STRONG>
<A HREF="copyright.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/copyright.html">Notice</A><WBR>
<P>
<I>Jun 17, 1998</I>
 </NOBR></FONT></CENTER></BODY></HTML>


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -