Excel VBA 正则表达式匹配位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8301622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:34:16  来源:igfitidea点击:

Excel VBA Regex Match Position

regexexcelexcel-vbavba

提问by Jonathan

How do I grab the position of the first matched result in a regular expression? See below.

如何在正则表达式中获取第一个匹配结果的位置?见下文。

Function MYMATCH(strValue As String, strPattern As String, Optional blnCase As Boolean = True, Optional blnBoolean = True) As String
    Dim objRegEx As Object
    Dim strPosition As Integer

    ' Create regular expression.
    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.Pattern = strPattern
    objRegEx.IgnoreCase = blnCase

    ' Do the search match.
    strPosition = objRegEx.Match(strValue)

    MYMATCH = strPosition
End Function

For one, I'm not entirely certain what .Matchis returning (string, integer, etc.). The one solution I found said I should create a Matchobject to and then grab the position from there, but unlike vb, vbadoes not recognize the Matchobject. I've also seen some codelike the following, but I'm not necessarily looking for the value, just the first string placement:

一方面,我不完全确定.Match返回的是什么(字符串、整数等)。我找到的一个解决方案说我应该创建一个Match对象,然后从那里获取位置,但与vb不同,vba无法识别该Match对象。我还看到了一些类似以下的代码,但我不一定要查找值,只是查找第一个字符串位置:

If allMatches.count <> 0 Then
    result = allMatches.Item(0).submatches.Item(0)
End If

Somewhat ignoring any of the possible syntax errors above (mostly due to me changing variable types right and left), how do I easily/simply accomplish this?

有点忽略上述任何可能的语法错误(主要是由于我左右更改了变量类型),我如何轻松/简单地完成此操作?

Thanks!

谢谢!

回答by brettdj

You can use FirstIndexto return the position of matches using the Executemethod, ie

您可以使用FirstIndexExecute方法返回匹配的位置,即

Function MYMATCH(strValue As String, strPattern As String, Optional blnCase As Boolean = True, Optional blnBoolean = True) As String
    Dim objRegEx As Object
    Dim strPosition As Integer
    Dim RegMC

    ' Create regular expression.
    Set objRegEx = CreateObject("VBScript.RegExp")
    With objRegEx
        .Pattern = strPattern
        .IgnoreCase = blnCase
        If .test(strValue) Then
            Set RegMC = .Execute(strValue)
            MYMATCH = RegMC(0).firstindex + 1
        Else
            MYMATCH = "no match"
        End If
    End With
End Function

Sub TestMe()
    MsgBox MYMATCH("test 1", "\d+")
End Sub

回答by Jonathan

For the benefit of others who may be having this problem, I finally figured it out.

为了可能遇到此问题的其他人的利益,我终于弄清楚了。

Option Explicit

Function CHAMATCH(strValue As String, strPattern As String, Optional blnCase As Boolean = True, Optional blnBoolean = True) As String
    Dim objRegEx As Object
    Dim objPosition As Object
    Dim strPosition As String

    ' Create regular expression.
    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.Pattern = strPattern
    objRegEx.IgnoreCase = blnCase

    ' Do the search match.
    Set objPosition = objRegEx.Execute(strValue)
    strPosition = objPosition(0).FirstIndex

    CHAMATCH = strPosition
End Function

Instead of a Matchtype, just a regular Objecttype will do (considering all it's returning is a class). Then, if you want to grab the index location, just use .FirstIndexon the match [of your choice], or if you want the value, us .Value

而不是一个Match类型,只是一个常规Object类型就可以了(考虑到它返回的只是一个类)。然后,如果您想获取索引位置,只需.FirstIndex在匹配 [您选择的] 上使用,或者如果您想要值,我们.Value