对于字符串“j2ee”我用“.*?(\d+)?.*”去匹配,匹配是匹配到了,为什么捕获的子匹配那个不见了?machs(0).value是j2ee,没有问题,但是machs(0).submatches(0)的值却是空的,例如这里我想得到的是2啊。有人可能会说你把数字的那个后面改成+,也就是“.*?(\d+)+.*”,这样machs(0).submatches(0)的值确实是2,但是如果原字符串是“jee”呢,这样的话machs(0).value的值都是空了。我的意思就是:
“j2ee”的话匹配的直接结果是“j2ee”,子匹配结果是“2”
“jee”的话匹配的直接结果是“jee”,子匹配结果是“”请问有办法办到吗?谢谢各位。

解决方案 »

  1.   

    去掉不行的,对于“jee”,根本什么都匹配不到了
    (\d+){1,0},要是可以这样就好了,优先进行匹配捕获,可是不行,楼上你试试就知道了
      

  2.   

    感觉它好像是尽量不存储似的,我用的createobject("VBScript.regexp")
      

  3.   

    不匹配换行符的话
    regex.Pattern = "[^\d\r\n]+(\d+)?.*"
      

  4.   

    楼上,谢谢了,上面的问题表面上是解决了,没想到简化问题是不行的实际应用是对一个表格里面数据分析的,问题的关键就是里面有些项目有有些项目没有,我的表达式如下:
    <span class="name"><a href=.*?>(.*?)</a></span>[\s\S]+?(<div class="props"><span>颜色: .*</span><span>尺码: .*</span></div>)?[\s\S]*?<td class="price">(.*?)</td>[\s\S]*?<td class="num">(.*?)</td>红色标记部分就是不确定的
    。表格中有n条数据,有些数据有“颜色、尺码”,有些没有,但是不管这项有还是没有都不能影响这个n条任一条数据的获取。现在的现象是跟1楼说的差不多。
    如果用?可以匹配到n条记录,但是子匹配,也就是“尺码颜色”一律捕获不到(得到内容为空)
    如果用+那么有“尺码颜色”记录是捕获到了,但是没有的“尺码颜色”就匹配不到了,最终匹配的记录<n(这里道理很明白+是匹配1次以上的,没有显然就匹配不到)
    如果两个都不用的话,那么类似上面(表示匹配1次,没有显然就匹配不到)
      

  5.   

    \d是表示数字,你要让它去匹配jee,它怎么匹配呢?得到的自然就是空的
      

  6.   

    因为
    都是非贪婪的所有优先级别是相同的
    它当然会从左到右匹配
    如果非要捕获数值
    不必局限思路可以换个式子
    javascript:alert('j2ee'.match(/\D+(?:(\d+).*)?/))
    地址栏运行
      

  7.   

    = =,少写了种情况。
    这样
    javascript:alert('j2ee'.match(/\D*(?:(\d+).*)?/))
    效率也会好很多
      

  8.   

    抱歉,刚刚理解错了= =.
    实在不好意思...
    仔细想了下.
    是这样的...
    j2ee
    .*?(\d+)?.*
    这里有3块
    .*?
    匹配到了^的位置(就是'')然后成立
    继续下面
    (\d+)?
    匹配到了^的位置(就是'')然后成立
    继续下面
    .*
    会一直匹配的到.不能匹配位置...由于没有\n,所以匹配到了末尾...
    然后匹配结束
      

  9.   

    原始数据太多,我做了份类似的。如下:
    <table cellspacing="0" cellpadding="0">
      <tr>
        <td class="item" colspan="2">商品名</td>
        <td class="price">单价</td>
        <td class="num">数量</td>
      </tr>
      <tr>
        <td class="item" colspan="2"><span class="name"><a href="#不定">商品A</a></span></td>
        <td class="price">5.00 </td>
        <td class="num">4 </td>
      </tr>
      <tr>
        <td class="item" colspan="2"><span class="name"><a href="#不定">商品B</a></span></td>
        <td class="price">5.00 </td>
        <td class="num">3 </td>
      </tr>
      <tr>
        <td class="item" colspan="2"><span class="name"><a href="#不定">商品C</a></span><span>颜色: 黑色 </span><span>尺码: 均码 </span></td>
        <td class="price">5.00 </td>
        <td class="num">2 </td>
      </tr>
      <tr>
        <td class="item" colspan="2"><span class="name"><a href="#不定">商品D</a></span></td>
        <td class="price">15.00 </td>
        <td class="num">1 </td>
      </tr>
    </table>需要取到里面的商品名、单价、数量,最关键的是有“颜色和尺码的也取出来”,一次匹配。我用的表达式如下:
    <span class="name"><a href=.*?>(.*?)</a></span>[\s\S]+?(<span>颜色: (.*?)</span><span>尺码: (.*?)</span>)?[\s\S]*?<td class="price">(.*?)</td>[\s\S]*?<td class="num">(.*?)</td>执行结果如下:
    1.<span class="name"><a href="#不定">商品A</a></span></td>
        <td class="price">5.00 </td>
        <td class="num">4 </td>
    (1).商品A (2). (3). (4). (5).5.00  (6).4  
    2.<span class="name"><a href="#不定">商品B</a></span></td>
        <td class="price">5.00 </td>
        <td class="num">3 </td>
    (1).商品B (2). (3). (4). (5).5.00  (6).3  
    3.<span class="name"><a href="#不定">商品C</a></span><span>颜色: 黑色 </span><span>尺码: 均码 </span></td>
        <td class="price">5.00 </td>
        <td class="num">2 </td>
    (1).商品C (2). (3). (4). (5).5.00  (6).2  
    4.<span class="name"><a href="#不定">商品D</a></span></td>
        <td class="price">15.00 </td>
        <td class="num">1 </td>
    (1).商品D (2). (3). (4). (5).15.00  (6).1  1表示直接匹配结果,(1)表示获取的子匹配。可以看到这里的第三条“(1).商品C (2). (3). (4). (5).5.00  (6).2”根本获取不到颜色和尺码。谢谢各位高手的回复,希望能解决问题。
      

  10.   

    <span class="name"><a href=.*?>(.*?)</a></span>[\s\S]+?(<span>颜色: (.*?)</span><span>尺码: (.*?)</span>|)[\s\S]*?<td class="price">(.*?)</td>[\s\S]*?<td class="num">(.*?)</td>
      

  11.   

    <span\b[^>]*><a\b[^>]*>(.*?)</a></span>(?:(?!<span>颜色|</td>)[\s\S])*(?:<span>颜色: (.*?)</span><span>尺码: (.*?)</span>)?[\s\S]*?<td class="price">(.*?)</td>[\s\S]*?<td class="num">(.*?)</td>
    Dim htmlCode, pattern, regex, matches, match, found
    htmlCode = "<table cellspacing=""0"" cellpadding=""0"">"
    htmlCode = htmlCode & vbCrLf & "  <tr>"
    htmlCode = htmlCode & vbCrLf & "    <td class=""item"" colspan=""2"">商品名</td>"
    htmlCode = htmlCode & vbCrLf & "    <td class=""price"">单价</td>"
    htmlCode = htmlCode & vbCrLf & "    <td class=""num"">数量</td>"
    htmlCode = htmlCode & vbCrLf & "  </tr>"
    htmlCode = htmlCode & vbCrLf & "  <tr>"
    htmlCode = htmlCode & vbCrLf & "    <td class=""item"" colspan=""2""><span class=""name""><a href=""#不定"">商品A</a></span></td>"
    htmlCode = htmlCode & vbCrLf & "    <td class=""price"">5.00</td>"
    htmlCode = htmlCode & vbCrLf & "    <td class=""num"">4</td>"
    htmlCode = htmlCode & vbCrLf & "  </tr>"
    htmlCode = htmlCode & vbCrLf & "  <tr>"
    htmlCode = htmlCode & vbCrLf & "    <td class=""item"" colspan=""2""><span class=""name""><a href=""#不定"">商品B</a></span></td>"
    htmlCode = htmlCode & vbCrLf & "    <td class=""price"">5.00</td>"
    htmlCode = htmlCode & vbCrLf & "    <td class=""num"">3</td>"
    htmlCode = htmlCode & vbCrLf & "  </tr>"
    htmlCode = htmlCode & vbCrLf & "  <tr>"
    htmlCode = htmlCode & vbCrLf & "    <td class=""item"" colspan=""2""><span class=""name""><a href=""#不定"">商品C</a></span><span>颜色: 黑色</span><span>尺码: 均码</span></td>"
    htmlCode = htmlCode & vbCrLf & "    <td class=""price"">5.00</td>"
    htmlCode = htmlCode & vbCrLf & "    <td class=""num"">2</td>"
    htmlCode = htmlCode & vbCrLf & "  </tr>"
    htmlCode = htmlCode & vbCrLf & "  <tr>"
    htmlCode = htmlCode & vbCrLf & "    <td class=""item"" colspan=""2""><span class=""name""><a href=""#不定"">商品D</a></span></td>"
    htmlCode = htmlCode & vbCrLf & "    <td class=""price"">15.00</td>"
    htmlCode = htmlCode & vbCrLf & "    <td class=""num"">1</td>"
    htmlCode = htmlCode & vbCrLf & "  </tr>"
    htmlCode = htmlCode & vbCrLf & "</table>" pattern = "<span\b[^>]*><a\b[^>]*>(.*?)</a></span>(?:(?!<span>颜色|</td>)[\s\S])*(?:<span>颜色: (.*?)</span><span>尺码: (.*?)</span>)?[\s\S]*?<td class=""price"">(.*?)</td>[\s\S]*?<td class=""num"">(.*?)</td>"Set regex        = New RegExp
    regex.Pattern    = pattern
    regex.Global     = True
    regex.IgnoreCase = True
    Set matches      = regex.Execute(htmlCode)For Each match in matches
    found = ""
    If match.Submatches(0)<>"" Then found = found & " " & match.Submatches(0)
    If match.Submatches(1)<>"" Then found = found & " " & match.Submatches(1)
    If match.Submatches(2)<>"" Then found = found & " " & match.Submatches(2)
    If match.Submatches(3)<>"" Then found = found & " " & match.Submatches(3)
    If match.Submatches(4)<>"" Then found = found & " " & match.Submatches(4)
    Response.Write found & "<br />"
    '...
    Next
    Set matches = Nothing
    Set regex   = Nothing
      

  12.   

    非常感谢楼上,尽管昨天我已经用了二次匹配解决了。(?:(?!<span>颜色|</td>)[\s\S])* 这个东西很神奇啊,是强制让它去匹配吧?先结贴,不多说了,再次感谢,也谢谢其他回答的高手!