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

📄 ch01_14.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 1.13. Escaping Characters (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:29:18Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch01_01.htm"TITLE="1. Strings"><LINKREL="prev"HREF="ch01_13.htm"TITLE="1.12. Reformatting Paragraphs"><LINKREL="next"HREF="ch01_15.htm"TITLE="1.14. Trimming Blanks from the Ends of a String"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch01_13.htm"TITLE="1.12. Reformatting Paragraphs"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 1.12. Reformatting Paragraphs"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch01_01.htm"TITLE="1. Strings"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch01_15.htm"TITLE="1.14. Trimming Blanks from the Ends of a String"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 1.14. Trimming Blanks from the Ends of a String"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch01-23292">1.13. Escaping Characters</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch01-pgfId-1523">Problem <ACLASS="indexterm"NAME="ch01-idx-1000010315-0"></A><ACLASS="indexterm"NAME="ch01-idx-1000010315-1"></A><ACLASS="indexterm"NAME="ch01-idx-1000010315-2"></A></A></H3><PCLASS="para">You need to output a string with certain characters (quotes, commas, etc.) escaped. For instance, you're producing a format string for <CODECLASS="literal">sprintf</CODE> and want to convert literal <CODECLASS="literal">%</CODE> signs into <CODECLASS="literal">%%</CODE>.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch01-pgfId-1529">Solution</A></H3><PCLASS="para">Use a substitution to backslash or double each character to be escaped. <ACLASS="indexterm"NAME="ch01-idx-1000010316-0"></A><ACLASS="indexterm"NAME="ch01-idx-1000010316-1"></A></P><PRECLASS="programlisting"># backslash$var =~ s/([CHARLIST])/\\$1/g;# double$var =~ s/([CHARLIST])/$1$1/g;</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch01-pgfId-1545">Discussion</A></H3><PCLASS="para"><CODECLASS="literal">$var</CODE> is the variable to be altered. The <CODECLASS="literal">CHARLIST</CODE> is a list of characters to escape and can contain backslash escapes like <CODECLASS="literal">\t</CODE> and <CODECLASS="literal">\n</CODE>. If you just have one character to escape, omit the brackets:</P><PRECLASS="programlisting">$string =~ s/%/%%/g;</PRE><PCLASS="para">The following lets you do escaping when preparing strings to submit to the shell. (In practice, you would need to escape more than just <CODECLASS="literal">'</CODE> and &quot; to make any arbitrary string safe for the shell. Getting the list of characters right is so hard, and the risks if you get it wrong are so great, that you're better off using the list form of <CODECLASS="literal">system</CODE> and <CODECLASS="literal">exec</CODE> to run programs, shown in <ACLASS="xref"HREF="ch16_03.htm"TITLE="Running Another Program">Recipe 16.2</A>. They avoid the shell altogether.)</P><PRECLASS="programlisting">$string = q(Mom said, &quot;Don't do that.&quot;);$string =~ s/(['&quot;])/\\$1/g;</PRE><PCLASS="para">We had to use two backslashes in the replacement because the replacement section of a substitution is read as a double-quoted string, and to get one backslash, you need to write two. Here's a similar example for VMS DCL, where you need to double every quote to get one through:</P><PRECLASS="programlisting">$string = q(Mom said, &quot;Don't do that.&quot;);$string =~ s/(['&quot;])/$1$1/g;</PRE><PCLASS="para">Microsoft <ACLASS="indexterm"NAME="ch01-idx-1000010327-0"></A>command interpreters are harder to work with. In DOS and Windows <EMCLASS="emphasis">COMMAND.COM</EM> recognizes double quotes but not single ones, has no clue what to do with backquotes, and requires a backslash to make a double quote a literal. Almost any of the free or commercial Unix-like shell environments for Windows will improve this depressing situation.</P><PCLASS="para">Because we're using character classes in the regular expressions, we can use <CODECLASS="literal">-</CODE> to define a range, and <CODECLASS="literal">^</CODE> at the start to negate. This escapes all characters that aren't in the range A through Z.</P><PRECLASS="programlisting">$string =~ s/([^A-Z])/\\$1/g;</PRE><PCLASS="para">If you want to escape all non-word characters, use the <CODECLASS="literal">\Q</CODE> and <CODECLASS="literal">\E</CODE> string metacharacters or the <CODECLASS="literal">quotemeta</CODE><ACLASS="indexterm"NAME="ch01-idx-1000010328-0"></A><ACLASS="indexterm"NAME="ch01-idx-1000010328-1"></A><ACLASS="indexterm"NAME="ch01-idx-1000010328-2"></A> function. For example, these are equivalent:</P><PRECLASS="programlisting">$string = &quot;this \Qis a test!\E&quot;;$string = &quot;this is\\ a\\ test\\!&quot;;$string = &quot;this &quot; . quotemeta(&quot;is a test!&quot;);</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch01-pgfId-1579">See Also</A></H3><PCLASS="para">The <CODECLASS="literal">s///</CODE> operator in <EMCLASS="emphasis">perlre </EM>(1) and <EMCLASS="emphasis">perlop </EM>(1) and the <ACLASS="olink"HREF="../prog/ch02_04.htm">"Pattern Matching"</A> section of <ACLASS="olink"HREF="../prog/ch02_01.htm">Chapter 2</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; the <CODECLASS="literal">quotemeta</CODE> function in <EMCLASS="emphasis">perlfunc </EM>(1) and <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; the discussion of HTML escaping in <ACLASS="xref"HREF="ch19_02.htm"TITLE="Writing a CGI Script">Recipe 19.1</A>; <ACLASS="xref"HREF="ch19_07.htm"TITLE="Executing Commands Without Shell Escapes">Recipe 19.6</A> for how to avoid having to escape strings to give the shell<ACLASS="indexterm"NAME="ch01-idx-1000010323-0"></A><ACLASS="indexterm"NAME="ch01-idx-1000010323-1"></A><ACLASS="indexterm"NAME="ch01-idx-1000010323-2"></A> <ACLASS="indexterm"NAME="ch01-idx-1000010318-0"></A><ACLASS="indexterm"NAME="ch01-idx-1000010318-1"></A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch01_13.htm"TITLE="1.12. Reformatting Paragraphs"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 1.12. Reformatting Paragraphs"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch01_15.htm"TITLE="1.14. Trimming Blanks from the Ends of a String"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 1.14. Trimming Blanks from the Ends of a String"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">1.12. Reformatting Paragraphs</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">1.14. Trimming Blanks from the Ends of a String</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

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