[問題] VBA 2010的regular expression如何非貪婪

作者: gecer (gecer)   2014-11-12 22:58:48
/* 聽說註明使用的語言、環境
* 問題有可能較容易被解決...
*/
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 字串 請問要如何更正?
作者: LPH66 (-6.2598534e+18f)   2014-11-12 23:33:00
非貪婪只能從由左到右找最短的組合, 由右到左的要用別招你的問題可能應該要寫出「123, 跟著一串字當中不能有 123,然後 abc」才行, 不過我一下子想不到中間那個要怎麼寫就是
作者: lin1987www (林奕忠)   2014-11-25 04:48:00
我覺得可以檢查是否有沒有包含 123 字串做檢查123((?!123).)*abc 這樣每次在吃字的時候都會判斷右邊是不是有 123 這樣應該可以達到你要的效果 :)

Links booklink

Contact Us: admin [ a t ] ucptt.com