Regex 查找一个模式的两个或多个出现点;“提交报告”;或;“已提交报告”;使用vba正则表达式
我是vba正则表达式新手,我需要在一个非常大的报告中查找字段,其中文本字段或文本单元格中的文本包含以下模式的3个或更多实例: 使用vba正则表达式“提交报告”或“提交报告”或“提交代表”或“重新提交代表”或“重新提交报告”,不区分大小写 我无法计算正则表达式和条件计数,因此可以将其调整为2、3或更多Regex 查找一个模式的两个或多个出现点;“提交报告”;或;“已提交报告”;使用vba正则表达式,regex,vba,excel,Regex,Vba,Excel,我是vba正则表达式新手,我需要在一个非常大的报告中查找字段,其中文本字段或文本单元格中的文本包含以下模式的3个或更多实例: 使用vba正则表达式“提交报告”或“提交报告”或“提交代表”或“重新提交代表”或“重新提交报告”,不区分大小写 我无法计算正则表达式和条件计数,因此可以将其调整为2、3或更多 任何指导都将不胜感激。我不确定如何使用RegEx实现这一点,以下肯定不是解决此问题的最佳方法。但它确实起到了作用。希望这有助于: Option Compare Text Public Sub Se
任何指导都将不胜感激。我不确定如何使用RegEx实现这一点,以下肯定不是解决此问题的最佳方法。但它确实起到了作用。希望这有助于:
Option Compare Text
Public Sub SearchForMultipleOccurencesOfReport()
Dim bolFoundSomehing As Boolean
Dim strLargeReport As String
Dim lngCounter As Long
Dim lngLooper As Long
Dim z As Variant
intCounter = 0
strLargeReport = "This is the text of your large report containing all these searched terms like submit report, submitting rep, or resubmitting report."
For lngLooper = 1 To 100000
bolFoundSomehing = False
z = InStr(1, strLargeReport, "submit report")
If IsNumeric(z) And z > 0 Then
lngCounter = lngCounter + 1
bolFoundSomehing = True
strLargeReport = Mid(strLargeReport, InStr(1, strLargeReport, "submit report") + Len("submit report"))
End If
z = InStr(1, strLargeReport, "submitted report")
If IsNumeric(z) And z > 0 Then
lngCounter = lngCounter + 1
bolFoundSomehing = True
strLargeReport = Mid(strLargeReport, InStr(1, strLargeReport, "submitted report") + Len("submitted report"))
End If
z = InStr(1, strLargeReport, "submitting rep")
If IsNumeric(z) And z > 0 Then
lngCounter = lngCounter + 1
bolFoundSomehing = True
strLargeReport = Mid(strLargeReport, InStr(1, strLargeReport, "submitting rep") + Len("submitting rep"))
End If
z = InStr(1, strLargeReport, "resubmitted rep")
If IsNumeric(z) And z > 0 Then
lngCounter = lngCounter + 1
bolFoundSomehing = True
strLargeReport = Mid(strLargeReport, InStr(1, strLargeReport, "resubmitted rep") + Len("resubmitted rep"))
End If
z = InStr(1, strLargeReport, "resubmitting report")
If IsNumeric(z) And z > 0 Then
lngCounter = lngCounter + 1
bolFoundSomehing = True
strLargeReport = Mid(strLargeReport, InStr(1, strLargeReport, "resubmitting report") + Len("resubmitting report"))
End If
If bolFoundSomehing = False Then Exit For
Next lngLooper
MsgBox "Found " & lngCounter & " occurences of the searched terms in the report."
End Sub
我根本不熟悉VBA,也不熟悉VBA如何处理正则表达式的细节,但这个正则表达式应该与您在问题中提到的所有术语相匹配:
/(?:re)?submit(?:ted|ting)? rep(?:ort)?/i
这将匹配您提到的任何字符串的一个实例。您可以使用RegEx子例程或递归来匹配此模式的多个实例,但我不知道VBA是否支持这些。这是一个丑陋但实用的解决方案:
/(?:re)?submit(?:ted|ting)? rep(?:ort)?.+?(?:re)?submit(?:ted|ting)? rep(?:ort)?/i
这就是上面重复两次的同一个正则表达式,它们之间有一个+?
。这将导致正则表达式匹配您要查找的短语的第一个和第二个实例之间的任何和所有字符。如果有匹配项,则表示文本中至少有两个该短语的实例