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

📄 ch26_05.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 26] 26.5 Getting Regular Expressions Right </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:44:07Z"><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_04.htm"TITLE="26.4 Using Metacharacters in Regular Expressions "><LINKREL="next"HREF="ch26_06.htm"TITLE="26.6 Just What Does a Regular Expression Match? "></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_04.htm"TITLE="26.4 Using Metacharacters in Regular Expressions "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 26.4 Using Metacharacters in Regular Expressions "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_06.htm"TITLE="26.6 Just What Does a Regular Expression Match? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 26.6 Just What Does a Regular Expression Match? "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7949">26.5 Getting Regular Expressions Right </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-28986"></A>Writing regular expressions involves more than learning the mechanics.You not only haveto learn how to describe patterns, you also have to recognizethe context in which they appear.You have to be able to think through the level ofdetail that is necessary in a regular expression, basedon the context in which the pattern will be applied.</P><PCLASS="para">The same thing that makes writing regular expressions difficult is what makes writing them interesting:the variety of occurrences or contexts in which a pattern appears.This complexity is inherent in language itself, just as you can't always understand an<SPANCLASS="link">expression (<ACLASS="linkend"HREF="ch26_01.htm#UPT-ART-7844"TITLE="That's an Expression ">26.1</A>)</SPAN>by looking up each word in the dictionary. </P><PCLASS="para">The process of writing a regular expression involvesthree steps:</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">Knowing what it is you want to match and how it might appear in the text. </P></LI><LICLASS="listitem"><PCLASS="para">Writing a pattern to describe what you want to match.</P></LI><LICLASS="listitem"><PCLASS="para">Testing the pattern to see what it matches.</P></LI></OL><PCLASS="para">This process is virtually the same kind of process that a programmerfollows to develop a program.Step 1 might be considered the specification,which should reflect an understanding of the problem to be solved as wellas how to solve it.Step 2 is analogous to the actual coding of the program,and step 3 involves running the program and testing it against the specification.Steps 2 and 3 form a loop that is repeated until the programworks satisfactorily.</P><PCLASS="para">Testing your description of what you want to match ensures that the descriptionworks as expected.It usually uncovers a few surprises.Carefully examining the results of a test, comparing the outputagainst the input, will greatly improve yourunderstanding of regular expressions.You might consider evaluating theresults of a pattern-matching operation as follows: </P><DLCLASS="variablelist"><DTCLASS="term">Hits</DT><DDCLASS="listitem"><PCLASS="para"><EMCLASS="emphasis">The lines that I wanted to match.</EM></P></DD><DTCLASS="term">Misses</DT><DDCLASS="listitem"><PCLASS="para"><EMCLASS="emphasis">The lines that I didn't want to match.</EM></P></DD><DTCLASS="term">Misses that should be hits</DT><DDCLASS="listitem"><PCLASS="para"><EMCLASS="emphasis">The lines that I didn't match but wanted to match.</EM></P></DD><DTCLASS="term">Hits that should be misses</DT><DDCLASS="listitem"><PCLASS="para"><EMCLASS="emphasis">The lines that I matched but didn't want to match.</EM></P></DD></DL><PCLASS="para">Trying to perfect your description of a pattern is something that you work at from opposite ends: you try to eliminate the &quot;hits that should be misses&quot;by limiting the possible matches and you try to capture the &quot;misses that should be hits&quot; by expandingthe possible matches.</P><PCLASS="para">The difficulty is especially apparent when you must describe patterns using fixed strings. Each character you remove from the fixed-string pattern increases the number of possible matches.For instance, while searching for the string <CODECLASS="literal">what</CODE>,you determine that you'd like to match <CODECLASS="literal">What</CODE> as well.The only fixed-string pattern that will match <CODECLASS="literal">What</CODE> and <CODECLASS="literal">what</CODE> is <CODECLASS="literal">hat</CODE>, the longest string common to both. It is obvious, though, that searching for <CODECLASS="literal">hat</CODE> willproduce unwanted matches.Each character you add to a fixed-string pattern decreasesthe number of possible matches.The string <CODECLASS="literal">them</CODE> is going to produce fewer matches than the string <CODECLASS="literal">the</CODE>.</P><PCLASS="para">Using metacharacters in patterns provides greater flexibility in extending or narrowing the range of matches.Metacharacters, used in combination with literalsor other metacharacters, can be used to expand the range of matcheswhile still eliminating the matches that you do not want.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DD</SPAN> <SPANCLASS="bibliomisc">from O'Reilly &amp; Associates' <CITECLASS="citetitle">sed &amp; 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_04.htm"TITLE="26.4 Using Metacharacters in Regular Expressions "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 26.4 Using Metacharacters in Regular Expressions "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_06.htm"TITLE="26.6 Just What Does a Regular Expression Match? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 26.6 Just What Does a Regular Expression Match? "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">26.4 Using Metacharacters in Regular Expressions </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.6 Just What Does a Regular Expression Match? </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 &amp; 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 + -