Regex 如何在VBA中获得子匹配的位置?
我需要得到子匹配字符串的索引位置值。根据文档,我已经通读了这篇文章,了解了Regex 如何在VBA中获得子匹配的位置?,regex,vba,vbscript,macros,ms-word,Regex,Vba,Vbscript,Macros,Ms Word,我需要得到子匹配字符串的索引位置值。根据文档,我已经通读了这篇文章,了解了FirstIndex属性以获取匹配字符串的位置 但这只适用于一维匹配字符串。我无法为子匹配应用FirstIndex。 请参考 我试过这种格式 Dim myRegExp As Object, match As MatchCollection Dim matched As String Set myRegExp = CreateObject("VBScri
FirstIndex
属性以获取匹配字符串的位置
但这只适用于一维匹配字符串。我无法为子匹配应用FirstIndex
。
请参考
我试过这种格式
Dim myRegExp As Object, match As MatchCollection
Dim matched As String
Set myRegExp = CreateObject("VBScript.RegExp")
myRegExp.pattern = find
If myRegExp.test(text) = True Then
Set match = myRegExp.Execute(text)
Debug.Print match(0).submatches(0) '' this is matched string
我应该在哪里调用FirstIndex
来获取子匹配字符串的位置
输出:
match(0)=>Berry, Brent. (2006). What accounts for race and ethnic differences in Berry,
Brent. parental financial transfers to adult children in the United States? Journal of Family
Issues 37:1583-1604.
submatches(0)=>Berry, Brent.
submatches(6)=>2006
预期产出:
submatches(0) at 0th position
submatches(6) at 16th position and so on
无法将
.FirstIndex
应用于子匹配(x)
,因为它返回的是字符串,而不是匹配项。如果组将返回唯一匹配项,只需使用Instr
功能即可找到其位置:
With CreateObject("VBScript.RegExp")
.Pattern = Find
If .Test(text) Then
Set match = .Execute(text)
Debug.Print InStr(1, text, match(0).SubMatches(0)) '0
Debug.Print InStr(1, text, match(0).SubMatches(5)) '16
'and so on
End If
End With
如果组不会返回唯一的结果,则可以跟踪最后一次匹配的位置并循环搜索结果。请注意,VBScript.RegExp
不支持look behinds,因此不必考虑匹配的长度:
With CreateObject("VBScript.RegExp")
.Pattern = find
If .Test(text) Then
Set match = .Execute(text)
Dim i As Long, pos As Long, found As String
pos = 1
For i = 0 To match(0).SubMatches.Count - 1
found = match(0).SubMatches(i)
pos = InStr(pos, text, match(0).SubMatches(i))
Debug.Print found, pos
Next
End If
End With
无法将.FirstIndex
应用于子匹配(x)
,因为它返回的是字符串,而不是匹配项。如果组将返回唯一匹配项,只需使用Instr
功能即可找到其位置:
With CreateObject("VBScript.RegExp")
.Pattern = Find
If .Test(text) Then
Set match = .Execute(text)
Debug.Print InStr(1, text, match(0).SubMatches(0)) '0
Debug.Print InStr(1, text, match(0).SubMatches(5)) '16
'and so on
End If
End With
如果组不会返回唯一的结果,则可以跟踪最后一次匹配的位置并循环搜索结果。请注意,VBScript.RegExp
不支持look behinds,因此不必考虑匹配的长度:
With CreateObject("VBScript.RegExp")
.Pattern = find
If .Test(text) Then
Set match = .Execute(text)
Dim i As Long, pos As Long, found As String
pos = 1
For i = 0 To match(0).SubMatches.Count - 1
found = match(0).SubMatches(i)
pos = InStr(pos, text, match(0).SubMatches(i))
Debug.Print found, pos
Next
End If
End With
包含以下字符串:
子匹配集合包含单个子匹配字符串
子匹配集合中的每个项都是找到的字符串,并且
由正则表达式捕获
因此无法获取位置/索引。包含字符串:
子匹配集合包含单个子匹配字符串
子匹配集合中的每个项都是找到的字符串,并且
由正则表达式捕获
因此,您无法获得位置/索引。您正在查找整个比赛的索引吗?只需使用匹配(0).FirstIndex
。你甚至不需要检查子匹配项
。不,我需要获得每个子匹配项的索引。我在我的帖子中添加了更多详细信息。你在寻找整个匹配项的索引吗?只需使用匹配(0).FirstIndex
。您甚至不需要检查子匹配项
。不,我需要获取每个子匹配字符串的索引。我在帖子中添加了更多详细信息。如果存在多个匹配项,并且子字符串在输入字符串中多次出现,该怎么办?@WiktorStribiżew-这将匹配的行为。FirstIndex
,虽然这确实假设组将返回唯一的匹配项。即使FirstIndex也只返回匹配字符串的第一次出现?@Learning-yes。它返回一个长的
,因此它只能返回一个结果-它返回第一个结果。哦!那么,如果字符串有重复项,那么哪种方法是获取字符串位置的有效方法呢?如果存在多个匹配项,并且子字符串在输入字符串中出现多次呢?@WiktorStribiżew-这将匹配.FirstIndex
,虽然这确实假设组将返回唯一的匹配项。即使FirstIndex也只返回匹配字符串的第一次出现?@Learning-yes。它返回一个长的
,因此它只能返回一个结果-它返回第一个结果。哦!所以,如果字符串有重复项,哪种方法是获取字符串位置的有效方法?谢谢!我不知道,谢谢你!我不知道这件事。