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

📄 ch06_15.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 6.14. Matching from Where the Last Pattern Left Off (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:34:33Z"><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="ch06_01.htm"TITLE="6. Pattern Matching"><LINKREL="prev"HREF="ch06_14.htm"TITLE="6.13. Approximate Matching"><LINKREL="next"HREF="ch06_16.htm"TITLE="6.15. Greedy and Non-Greedy Matches"></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="ch06_14.htm"TITLE="6.13. Approximate Matching"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 6.13. Approximate Matching"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch06_01.htm"TITLE="6. Pattern Matching"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch06_16.htm"TITLE="6.15. Greedy and Non-Greedy Matches"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 6.15. Greedy and Non-Greedy Matches"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch06-chap06_matching_4">6.14. Matching from Where the Last Pattern Left Off</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch06-pgfId-1599">Problem<ACLASS="indexterm"NAME="ch06-idx-1000007652-0"></A></A></H3><PCLASS="para">You want to match again from where the last pattern left off.</P><PCLASS="para">This is a useful approach to take when repeatedly extracting data in chunks from a string.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch06-pgfId-1607">Solution</A></H3><PCLASS="para">Use a combination of the <CODECLASS="literal">/g</CODE> match modifier, the <CODECLASS="literal">\G</CODE> pattern anchor, and the <CODECLASS="literal">pos</CODE> function.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch06-pgfId-1613">Discussion</A></H3><PCLASS="para">If you use the <CODECLASS="literal">/g</CODE><ACLASS="indexterm"NAME="ch06-idx-1000007653-0"></A><ACLASS="indexterm"NAME="ch06-idx-1000007653-1"></A> modifier on a match, the regular expression engine keeps track of its position in the string when it finished matching. The next time you match with <CODECLASS="literal">/g</CODE>, the engine starts looking for a match from this remembered position. This lets you use a <CODECLASS="literal">while</CODE> loop to extract the information you want from the string.</P><PRECLASS="programlisting">while (/(\d+)/g) {    print &quot;Found $1\n&quot;;}</PRE><PCLASS="para">You can also use <CODECLASS="literal">\G</CODE> in your pattern to anchor it to the end of the previous match. For example, if you had a number stored in a string with leading blanks, you could change each leading blank into the digit zero this way:</P><PRECLASS="programlisting">$n = &quot;   49 here&quot;;$n =~ s/\G /0/g;print $n;<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>00049 here</I></CODE></B></CODE></PRE><PCLASS="para">You can also make good use of <CODECLASS="literal">\G</CODE> in a <CODECLASS="literal">while</CODE> loop. Here we use <CODECLASS="literal">\G</CODE> to parse a comma-separated list of numbers (e.g., <CODECLASS="literal">&quot;3,4,5,9,120&quot;</CODE>):</P><PRECLASS="programlisting">while (/\G,?(\d+)/g) {    print &quot;Found number $1\n&quot;;}</PRE><PCLASS="para">By default, when your match fails (when we run out of numbers in the examples, for instance) the remembered position is reset to the start. If you don't want this to happen, perhaps because you want to continue matching from that position but with a different pattern, use the modifier <CODECLASS="literal">/c</CODE> with <CODECLASS="literal">/g</CODE>:</P><PRECLASS="programlisting">$_ = &quot;The year 1752 lost 10 days on the 3rd of September&quot;;while (/(\d+)/gc) {    print &quot;Found number $1\n&quot;;}if (/\G(\S+)/g) {    print &quot;Found $1 after the last number.\n&quot;;}<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Found number 1752</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Found number 10</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Found number 3</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Found rd after the last number.</I></CODE></B></CODE></PRE><PCLASS="para">As you can see, successive patterns can use <CODECLASS="literal">/g</CODE> on a string and in doing so change the location of the last successful match. The position of the last successful match is associated with the scalar being matched against, not with the pattern. Further, the position is not copied when you copy the string, nor saved if you use the ill-named <CODECLASS="literal">local</CODE> operator.</P><PCLASS="para">The location of the last successful match can be read and set with the <CODECLASS="literal">pos</CODE><ACLASS="indexterm"NAME="ch06-idx-1000007654-0"></A> function, which takes as its argument the string whose position you want to get or set. If no argument is given, <CODECLASS="literal">pos</CODE> operates on <CODECLASS="literal">$_ </CODE>:</P><PRECLASS="programlisting">print &quot;The position in \$a is &quot;, pos($a);pos($a) = 30;print &quot;The position in \$_ is &quot;, pos;pos = 30;<CODECLASS="literal"></CODE><ACLASS="indexterm"NAME="ch06-idx-1000007656-0"></A><ACLASS="indexterm"NAME="ch06-idx-1000007656-1"></A><ACLASS="indexterm"NAME="ch06-idx-1000007656-2"></A></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch06-pgfId-1685">See Also</A></H3><PCLASS="para">The <CODECLASS="literal">/g</CODE> modifier is discussed in <ICLASS="filename">perlre </I>(1) and the <ACLASS="olink"HREF="../prog/ch02_04.htm#PERL2-CH-2-SECT-4.1.2">"the rules of regular expression 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></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="ch06_14.htm"TITLE="6.13. Approximate Matching"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 6.13. Approximate Matching"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="ch06_16.htm"TITLE="6.15. Greedy and Non-Greedy Matches"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 6.15. Greedy and Non-Greedy Matches"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">6.13. Approximate Matching</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">6.15. Greedy and Non-Greedy Matches</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 + -