2013-04-18 39 views
1

我試圖根據1.度2.經驗的年數匹配各種職位的經驗水平。該模式非常簡單(例如:「BS/5」將是一個具有5年經驗的科學學士學位,我也有一些符合該方案的條目,但同一個字符串中有多個學位和經驗水平(例如:「BS/5-MS/2「)被認爲是等價的,我有一個基本函數可以匹配並找到子字符串模式,但是它永遠不會返回多於一個匹配,即使我已經將.Global屬性設置爲true來表示正則表達式?對象任何想法代碼如下:使用正則表達式在一個字符串中使用正則表達式的VBA多個匹配執行方法

On Error Resume Next 
ActiveWorkbook.VBProject.References.AddFromGuid "{3F4DACA7-160D-11D2-A8E9-00104B365C9F}", 5, 5 

Dim theRegex As Object 
Dim theString As String 

Set theRegex = CreateObject("VBScript.RegExp") 

With regex 
    .MultiLine = False 
    .Global = True 
    .IgnoreCase = False 
End With 

theRegex.Pattern = "([A-z][A-z][A-z]?/[0-9][0-9]?)" 

theString = "MS/9-PhD/4" 

Set MyMatches = theRegex.Execute(theString) 

Debug.Print "SubMatches.Count: " & MyMatches.Item(0).SubMatches.Count 

If MyMatches.Count <> 0 Then 
     With MyMatches 
      For myMatchCt = 0 To .Count - 1 
        Debug.Print "myMatchCt: " & myMatchCt 
        For subMtCt = 0 To .Item(subMtCt).SubMatches.Count - 1 
         Debug.Print "subMtCt: " & subMtCt 
         Debug.Print ("," & .Item(myMatchCt).SubMatches.Item(subMtCt)) 
        Next 
      Next 
     End With 
    Else 
    Debug.Print "No Matches" 
End If 
+0

最新評論。但是它看起來像是在第二個for循環中引用了錯誤的變量。 '對於subMtCt = 0 To .Item(subMtCt).SubMatches.Count - 1'應該可以是'For subMtCt = 0 To .Item(myMatchCt).SubMatches.Count - 1' –

回答

3

嘗試改變線With Regex

With theRegex 
    .MultiLine = False 
    .Global = True 
    .IgnoreCase = False 
End With 

您的On Error resume next聲明僞裝錯誤。

+0

謝謝!我是新來的遲到裝訂,我知道初學者的錯誤。對此,我真的非常感激! – user1902208

相關問題