📄 ch26_07.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 26] 26.7 Limiting the Extent of a Match </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:44:09Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-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="ch26_01.htm"TITLE="26. Regular Expressions (Pattern Matching)"><LINKREL="prev"HREF="ch26_06.htm"TITLE="26.6 Just What Does a Regular Expression Match? "><LINKREL="next"HREF="ch26_08.htm"TITLE="26.8 I Never Meta Character I Didn't Like "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch26_06.htm"TITLE="26.6 Just What Does a Regular Expression Match? "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 26.6 Just What Does a Regular Expression Match? "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 26<BR>Regular Expressions (Pattern Matching)</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch26_08.htm"TITLE="26.8 I Never Meta Character I Didn't Like "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 26.8 I Never Meta Character I Didn't Like "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7974">26.7 Limiting the Extent of a Match </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-29113"></A>A regular expression tries to matchthe longest string possible; that can causeunexpected problems.For instance, look at the following regular expression, which matches any number of characters inside of quotation marks:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">".*"</PRE></BLOCKQUOTE></P><PCLASS="para">Let's look at a <EMCLASS="emphasis">troff</EM> macrothat has two quoted arguments, as shown below:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">.Se "Appendix" "Full Program Listings"</PRE></BLOCKQUOTE></P><PCLASS="para">To match the first argument,a novice might describe the pattern with thefollowing regular expression:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">\.Se ".*"</PRE></BLOCKQUOTE></P><PCLASS="para">However, the pattern ends up matching the whole line because the secondquotation mark in the pattern matches the last quotation mark on theline.If you know how many arguments there are, you can specify eachof them:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">\.Se ".*" ".*"</PRE></BLOCKQUOTE></P><PCLASS="para">Although this works as you'd expect, each line might not have thesame number of arguments, causing misses that should be hits - you simply want the first argument.Here's a differentregular expression that matchesthe shortest possible extent between two quotation marks:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">"[^"]*"</PRE></BLOCKQUOTE></P><PCLASS="para">It matches "a quote, followed by any number of characters thatdo not match a quote, followed by a quote."The use of what we might call "negated character classes" like this is one of the things that distinguishes the journeyman regular expression user from the novice.[<SPANCLASS="link">Perl 5 (<ACLASS="linkend"HREF="ch37_05.htm"TITLE="And Now, Perl 5 ">37.5</A>)</SPAN>has added a new "non-greedy" regular expression operator that matchesthe shortest string possible. -<EMCLASS="emphasis">JP</EM> ]</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DD</SPAN> <SPANCLASS="bibliomisc">from O'Reilly & Associates' <CITECLASS="citetitle">sed & awk</CITE></SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch26_06.htm"TITLE="26.6 Just What Does a Regular Expression Match? "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 26.6 Just What Does a Regular Expression Match? "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch26_08.htm"TITLE="26.8 I Never Meta Character I Didn't Like "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 26.8 I Never Meta Character I Didn't Like "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">26.6 Just What Does a Regular Expression Match? </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">26.8 I Never Meta Character I Didn't Like </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -