Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何返回vbscript正则表达式捕获组的第2到第n个匹配项_Regex_Vba_Excel - Fatal编程技术网

Regex 如何返回vbscript正则表达式捕获组的第2到第n个匹配项

Regex 如何返回vbscript正则表达式捕获组的第2到第n个匹配项,regex,vba,excel,Regex,Vba,Excel,我有一个正则表达式,它匹配一个特定的文本字符串,然后应该返回与匹配字符串相邻的单元格中的匹配项。我正在使用一个捕获组,因此可能有多个匹配。我可以毫无问题地返回第一场比赛,但我不知道如何返回第二场到第n场比赛 我的代码如下: Sub splitUpComments() Dim regEx As New RegExp Dim strPattern As String Dim strInput As String Dim strReplace As String

我有一个正则表达式,它匹配一个特定的文本字符串,然后应该返回与匹配字符串相邻的单元格中的匹配项。我正在使用一个捕获组,因此可能有多个匹配。我可以毫无问题地返回第一场比赛,但我不知道如何返回第二场到第n场比赛

我的代码如下:

Sub splitUpComments()
    Dim regEx As New RegExp
    Dim strPattern As String
    Dim strInput As String
    Dim strReplace As String
    Dim Myrange As Range

    Set Myrange = ActiveSheet.Range("G2:G5")

    For Each c In Myrange
        strPattern = "(\S.+?[.?!])(?=\s+|$)"
        If strPattern <> "" Then
            strInput = c.Value
            strReplace = "$1"

            With regEx
                .Global = True
                .MultiLine = True
                .IgnoreCase = False
                .Pattern = strPattern
            End With

            If regEx.test(strInput) Then
                c.Offset(0, 1) = regEx.Replace(strInput, "$1")
                c.Offset(0, 2) = regEx.Replace(strInput, "$2")
                c.Offset(0, 3) = regEx.Replace(strInput, "$3")
            Else
                'do nothing
            End If
        End If
    Next
End Sub
我希望看到:

  • 这是一个测试。这是测试二。这是测试三
您可以看到正则表达式在Regex 101中工作:

但我得到的是:

This is a test one. This is a test two. This is a test three.       $2$2$2      $3$3$3
(其中第一个单元格包含整个目标字符串,接下来的两列分别包含$2$2$2和$3$3$3。)


看起来,(1)正则表达式不起作用,(2)$2和$3表示第二个和第三个捕获组,而不是第一个捕获组的第二个和第三个实例。有人能解释一下吗。谢谢。

很抱歉,我的vba知识有限,但您可以按照以下方法进行操作

i = 1
set MyMatches = regEx.test(strInput)
for each match in MyMatches
    c.Offset(0, i) = match
    i++
next

您可以使用Match对象访问匹配,如alpha bravo在回答中所示。只能使用RexEx对象的Execute方法创建有效的match对象。我的原始代码中缺少Execute。在下面的代码中,我将文本替换为Execute,并使用.Value方法访问Match对象中的值

            Set MyMatches = regEx.Execute(strInput)
            match_count = 0
            For Each Match In MyMatches
                match_count = match_count + 1
                c.Offset(0, match_count) = Match.Value
            Next

希望这能有所帮助。

也许我搞错了(2)。如果不是:不管你多久引用一次相同的匹配-你总是使用相同的(好的)引用。(否则您将如何访问其他匹配项?)。你让我去调查匹配对象。这是一个匹配项的集合,您可以使用for each(如您所示)以及value修饰符来引用每个值。例如Match.value。
            Set MyMatches = regEx.Execute(strInput)
            match_count = 0
            For Each Match In MyMatches
                match_count = match_count + 1
                c.Offset(0, match_count) = Match.Value
            Next