正则表达式中 .* 与 .*? 的区别

来源:互联网 发布:苹果手机九格切图软件 编辑:程序博客网 时间:2024/05/28 15:26

原文链接:https://leohowell.com/posts/regex-diff-point-asterisk-question-mark/

正则表达式中 .* 与 .*? 的区别, 简单说是贪婪匹配与非贪婪匹配的区别。 —— 由leohowell分享

简单说是贪婪匹配非贪婪匹配的区别。

比如说匹配输入串A: 101000000000100

  1. 使用 1.*1 将会匹配到1010000000001, 匹配方法: 先匹配至输入串A的最后, 然后向前匹配, 直到可以匹配到1, 称之为贪婪匹配。

  2. 使用 1.*?1 将会匹配到101, 匹配方法: *匹配下一个1之前的所有字符, 称之为非贪婪匹配。

所有带有量词的都是非贪婪匹配: .*?, .+?, .{2,6}? 甚至 .??

<table bordercolor="#cccccc" cellspacing="1" cellpadding="2" width="778" border="1">
  <tbody>
  <tr>
    <th>字符</th>
    <th>描述</th></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\</th>
    <td>将下一个字符标记为一个特殊字符、或一个原义字符、或一个向后引用、或一个八进制转义符。例如,“<code>n</code>”匹配字符“<code>n</code>”。“<code>\n</code>”匹配一个换行符。序列“<code>\\</code>”匹配“<code>\</code>”而“<code>\(</code>”则匹配“<code>(</code>”。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">^</th>
    <td>匹配输入字符串的开始位置。如果设置了RegExp对象的Multiline属性,^也匹配“<code>\n</code>”或“<code>\r</code>”之后的位置。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">$</th>
    <td>匹配输入字符串的结束位置。如果设置了RegExp对象的Multiline属性,$也匹配“<code>\n</code>”或“<code>\r</code>”之前的位置。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">*</th>
    <td>匹配前面的子表达式零次或多次。例如,zo*能匹配“<code>z</code>”以及“<code>zoo</code>”。*等价于{0,}。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">+</th>
    <td>匹配前面的子表达式一次或多次。例如,“<code>zo+</code>”能匹配“<code>zo</code>”以及“<code>zoo</code>”,但不能匹配“<code>z</code>”。+等价于{1,}。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">?</th>
    <td>匹配前面的子表达式零次或一次。例如,“<code>do(es)?</code>”可以匹配“<code>do</code>”或“<code>does</code>”中的“<code>do</code>”。?等价于{0,1}。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">{<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>}</th>
    <td><span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>是一个非负整数。匹配确定的<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>次。例如,“<code>o{2}</code>”不能匹配“<code>Bob</code>”中的“<code>o</code>”,但是能匹配“<code>food</code>”中的两个o。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">{<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>,}</th>
    <td><span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>是一个非负整数。至少匹配<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>次。例如,“<code>o{2,}</code>”不能匹配“<code>Bob</code>”中的“<code>o</code>”,但能匹配“<code>foooood</code>”中的所有o。“<code>o{1,}</code>”等价于“<code>o+</code>”。“<code>o{0,}</code>”则等价于“<code>o*</code>”。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">{<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>,<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">m</span>}</th>
    <td><span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">m</span>和<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>均为非负整数,其中<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>&lt;=<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">m</span>。最少匹配<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>次且最多匹配<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">m</span>次。例如,“<code>o{1,3}</code>”将匹配“<code>fooooood</code>”中的前三个o。“<code>o{0,1}</code>”等价于“<code>o?</code>”。请注意在逗号和两个数之间不能有空格。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">?</th>
    <td>当该字符紧跟在任何一个其他限制符(*,+,?,{<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>},{<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>,},{<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>,<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">m</span>})后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。例如,对于字符串“<code>oooo</code>”,“<code>o+?</code>”将匹配单个“<code>o</code>”,而“<code>o+</code>”将匹配所有“<code>o</code>”。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">.</th>
    <td>匹配除“<code>\</code><span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman"><code>n</code></span>”之外的任何单个字符。要匹配包括“<code>\</code><span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman"><code>n</code></span>”在内的任何字符,请使用像“<code>[.\</code><span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman"><code>n</code></span><code>]</code>”的模式。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">(pattern)</th>
    <td>匹配pattern并获取这一匹配。所获取的匹配可以从产生的Matches集合得到,在VBScript中使用SubMatches集合,在JScript中则使用$0…$9属性。要匹配圆括号字符,请使用“<code>\(</code>”或“<code>\)</code>”。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">(?:pattern)</th>
    <td>匹配pattern但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。这在使用或字符“<code>(|)</code>”来组合一个模式的各个部分是很有用。例如“<code>industr(?:y|ies)</code>”就是一个比“<code>industry|industries</code>”更简略的表达式。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">(?=pattern)</th>
    <td>正向预查,在任何匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如,“<code>Windows(?=95|98|NT|2000)</code>”能匹配“<code>Windows2000</code>”中的“<code>Windows</code>”,但不能匹配“<code>Windows3.1</code>”中的“<code>Windows</code>”。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">(?!pattern)</th>
    <td>负向预查,在任何不匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如“<code>Windows(?!95|98|NT|2000)</code>”能匹配“<code>Windows3.1</code>”中的“<code>Windows</code>”,但不能匹配“<code>Windows2000</code>”中的“<code>Windows</code>”。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">x|y</th>
    <td>匹配x或y。例如,“<code>z|food</code>”能匹配“<code>z</code>”或“<code>food</code>”。“<code>(z|f)ood</code>”则匹配“<code>zood</code>”或“<code>food</code>”。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">[xyz]</th>
    <td>字符集合。匹配所包含的任意一个字符。例如,“<code>[abc]</code>”可以匹配“<code>plain</code>”中的“<code>a</code>”。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">[^xyz]</th>
     <td>负值字符集合。匹配未包含的任意字符。例如,“<code>[^abc]</code>”可以匹配“<code>plain</code>”中的“<code>p</code>”。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">[a-z]</th>
    <td>字符范围。匹配指定范围内的任意字符。例如,“<code>[a-z]</code>”可以匹配“<code>a</code>”到“<code>z</code>”范围内的任意小写字母字符。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">[^a-z]</th>
     <td>负值字符范围。匹配任何不在指定范围内的任意字符。例如,“<code>[^a-z]</code>”可以匹配任何不在“<code>a</code>”到“<code>z</code>”范围内的任意字符。</td></tr>
  <tr>
    < th style="TEXT-ALIGN: center">\b</th>
    <td>匹配一个单词边界,也就是指单词和空格间的位置。例如,“<code>er\b</code>”可以匹配“<code>never</code>”中的“<code>er</code>”,但不能匹配“<code>verb</code>”中的“<code>er</code>”。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\B</th>
    <td>匹配非单词边界。“<code>er\B</code>”能匹配“<code>verb</code>”中的“<code>er</code>”,但不能匹配“<code>never</code>”中的“<code>er</code>”。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\cx</th>
    <td>匹配由x指明的控制字符。例如,\cM匹配一个Control-M或回车符。x的值必须为A-Z或a-z之一。否则,将c视为一个原义的“<code>c</code>”字符。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\d</th>
    <td>匹配一个数字字符。等价于[0-9]。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\D</th>
    <td>匹配一个非数字字符。等价于[^0-9]。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\f</th>
    <td>匹配一个换页符。等价于\x0c和\cL。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\n</th>
    <td>匹配一个换行符。等价于\x0a和\cJ。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\r</th>
    <td>匹配一个回车符。等价于\x0d和\cM。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\s</th>
    < td>匹配任何空白字符,包括空格、制表符、换页符等等。等价于[\f\n\r\t\v]。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\S</th>
    <td>匹配任何非空白字符。等价于[^\f\n\r\t\v]。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\t</th>
    <td>匹配一个制表符。等价于\x09和\cI。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\v</th>
    <td>匹配一个垂直制表符。等价于\x0b和\cK。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\w</th>
    <td>匹配包括下划线的任何单词字符。等价于“<code>[A-Za-z0-9_]</code>”。</td></tr>
  < tr>
    <th style="TEXT-ALIGN: center">\W</th>
    <td>匹配任何非单词字符。等价于“<code>[^A-Za-z0-9_]</code>”。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\x<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span></th>
    <td>匹配<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>,其中<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>为十六进制转义值。十六进制转义值必须为确定的两个数字长。例如,“<code>\x41</code>”匹配“<code>A</code>”。“<code>\x041</code>”则等价于“<code>\x04&amp;1</code>”。正则表达式中可以使用ASCII编码。.</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">num</span></th>
    <td>匹配<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">num</span>,其中<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">num</span>是一个正整数。对所获取的匹配的引用。例如,“<code>(.)\1</code>”匹配两个连续的相同字符。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span></th>
    <td>标识一个八进制转义值或一个向后引用。如果\<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>之前至少<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>个获取的子表达式,则<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>为向后引用。否则,如果<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>为八进制数字(0-7),则<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>为一个八进制转义值。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">nm</span></th>
    <td>标识一个八进制转义值或一个向后引用。如果\<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">nm</span>之前至少有<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">nm</span>个获得子表达式,则<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">nm</span>为向后引用。如果\<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">nm</span>之前至少有<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>个获取,则<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>为一个后跟文字<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">m</span>的向后引用。如果前面的条件都不满足,若<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>和<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">m</span>均为八进制数字(0-7),则\<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">nm</span>将匹配八进制转义值<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">nm</span>。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">nml</span></th>
    <td>如果<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>为八进制数字(0-3),且<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">m和l</span>均为八进制数字(0-7),则匹配八进制转义值<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">nm</span>l。</td></tr>
  <tr>
    <th style="TEXT-ALIGN: center">\u<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span></th>
    <td>匹配<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>,其中<span style="FONT-STYLE: italic; FONT-FAMILY: Times New Roman">n</span>是一个用四个十六进制数字表示的Unicode字符。例如,\u00A9匹配版权符号(?)。</td></tr></tbody></table>

参考链接

http://stackoverflow.com/questions/3075130/difference-between-and-for-regex

http://www.regular-expressions.info/repeat.html#lazy