作者gecer (gecer)
看板RegExp
标题[问题] VBA 2010的regular expression如何非贪婪
时间Wed Nov 12 22:58:48 2014
/*
听说注明使用的语言、环境
*
问题有可能较容易被解决...
*/
excel 2010 win7
code is as below
Option Explicit
Sub t()
Dim reg As Object
Dim s As String
Dim match As Object
Set reg = CreateObject("VBScript.RegExp")
reg.Pattern = "(123.*?abc)"
reg.ignoreCase = True
reg.MultiLine = False
reg.Global = True
s = "dfr123 123 1235abc"
Set match = reg.Execute(s)
End Sub
and the result is
http://i.imgur.com/VZda4lY.png
小弟想要抓到最短的1235abc 字串 请问要如何更正?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 111.255.27.116
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/RegExp/M.1415804330.A.9D3.html
1F:推 LPH66: 非贪婪只能从由左到右找最短的组合, 由右到左的要用别招 11/12 23:33
2F:→ LPH66: 你的问题可能应该要写出「123, 跟着一串字当中不能有 123, 11/12 23:34
3F:→ LPH66: 然後 abc」才行, 不过我一下子想不到中间那个要怎麽写就是 11/12 23:34
4F:→ lin1987www: 我觉得可以检查是否有没有包含 123 字串做检查 11/25 04:48
5F:→ lin1987www: 123((?!123).)*abc 这样每次在吃字的时候都会判断 11/25 04:50
6F:→ lin1987www: 右边是不是有 123 这样应该可以达到你要的效果 :) 11/25 04:50