📄 advancedtopics.html
字号:
<HTML><HEAD><TITLE> Regular Expressions </TITLE></HEAD><BODY><A NAME="Advanced_Topics"></A><H2> Advanced Topics </H2><P><H3>Substitutions</H3></P><P>Regular expression substitution can be used to program automatic editingoperations. For example, the following are search and replace strings to findoccurrences of the `C' language subroutine `get_x', reverse the first andsecond parameters, add a third parameter of NULL, and change the name to`new_get_x':</P><P><PRE> Search string: `get_x *\( *([^ ,]*), *([^\)]*)\)' Replace string: `new_get_x(\2, \1, NULL)'</PRE></P><P><H3>Ambiguity</H3></P><P>If a regular expression could match two different parts of the text, it willmatch the one which begins earliest. If both begin in the same place butmatch different lengths, or match the same length in different ways, lifegets messier, as follows.</P><P>In general, the possibilities in a list of alternatives are considered inleft-to-right order. The possibilities for `*', `+', and `?' are consideredlongest-first, nested constructs are considered from the outermost in, andconcatenated constructs are considered leftmost-first. The match that will bechosen is the one that uses the earliest possibility in the first choice thathas to be made. If there is more than one choice, the next will be made inthe same manner (earliest possibility) subject to the decision on the firstchoice. And so forth.</P><P>For example, `(ab|a)b*c' could match `abc' in one of two ways. The firstchoice is between `ab' and `a'; since `ab' is earlier, and does lead to asuccessful overall match, it is chosen. Since the `b' is already spoken for,the `b*' must match its last possibility, the empty string, since it mustrespect the earlier choice.</P><P>In the particular case where no `|'s are present and there is only one `*',`+', or `?', the net effect is that the longest possible match will bechosen. So `ab*', presented with `xabbbby', will match `abbbb'. Note thatif `ab*' is tried against `xabyabbbz', it will match `ab' just after `x', dueto the begins-earliest rule. (In effect, the decision on where to start thematch is the first choice to be made, hence subsequent choices must respectit even if this leads them to less-preferred alternatives.)</P><P><H3>References</H3></P><P>An excellent book on the care and feeding of regular expressions is</P><P><PRE> Mastering Regular Expressions, 2nd Edition Jeffrey E. F. Friedl 2002, O'Reilly & Associates ISBN 0-596-00289-0</PRE></P><P></P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -