2010-07-20 191 views

回答

1

正則表達式應該工作。

pattern = "[A][M][0-9]*[C][Q].*" 
string = "AM432CQ.txt" 
MsgBox RegExTest(pattern,string) 

Function RegExTest(pattern, strng) 
    Dim regEx, Match, Matches ' Create variable. 
    Set regEx = New RegExp ' Create a regular expression. 
    regEx.Pattern = pattern ' Set pattern. 
    regEx.IgnoreCase = True ' Set case insensitivity. 
    regEx.Global = True ' Set global applicability. 
    Set Matches = regEx.Execute(strng) ' Execute search. 
    For Each Match in Matches ' Iterate Matches collection. 
     RetStr = RetStr & "Match found at position " 
     RetStr = RetStr & Match.FirstIndex & ". Match Value is '" 
     RetStr = RetStr & Match.Value & "'." & vbCRLF 
    Next 
    RegExTest = RetStr 
End Function 
+0

非常感謝!!!!! – Min 2010-07-22 03:36:17

+0

不要寬恕c&p修正,但是'Set Matches = regEx.Execute(strng)'應該是'Set Matches = regEx.Execute(string)' – user66001 2017-03-11 18:25:51

+0

@ user66001 no。傳遞給函數的參數是'strng'。如果您進行了建議的更改,代碼將引發錯誤。 – Tester101 2017-03-11 20:21:50