Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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 VBA正则表达式-公式中使用的值的数据类型错误_Regex_Vba - Fatal编程技术网

Regex VBA正则表达式-公式中使用的值的数据类型错误

Regex VBA正则表达式-公式中使用的值的数据类型错误,regex,vba,Regex,Vba,我似乎不明白为什么这个包含正则表达式的函数总是返回错误数据类型的错误?我试图从excel文档中的文件路径字符串返回与已识别模式的匹配。我正在寻找的模式的一个示例是“H:\H1801100 MLK中学哈特福德\2-Archive!已发布的投标文件包\01文件包\U 2018-0905拆除和减排投标文件集\u图纸-PDF\00 HazMat\HM-1.PDF”样本字符串中的“02文件包\U 2018-1011”。下面列出了VBA代码的副本 Function textpart(Myrange As R

我似乎不明白为什么这个包含正则表达式的函数总是返回错误数据类型的错误?我试图从excel文档中的文件路径字符串返回与已识别模式的匹配。我正在寻找的模式的一个示例是“H:\H1801100 MLK中学哈特福德\2-Archive!已发布的投标文件包\01文件包\U 2018-0905拆除和减排投标文件集\u图纸-PDF\00 HazMat\HM-1.PDF”样本字符串中的“02文件包\U 2018-1011”。下面列出了VBA代码的副本

Function textpart(Myrange As Range) As Variant
Dim strInput As String
Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")

strInput = Myrange.Value

With regex
    .Pattern = "\D{2}\sPackage_\d{4}-\d{4}"
    .Global = True
End With

Set textpart = regex.Execute(strInput)

结束函数

您需要使用
\d{2}
来匹配2位区块,而不是
\d{2}
。此外,您正在尝试将整个匹配集合分配给函数结果,同时应提取第一个匹配值并将该值分配给函数结果:

Function textpart(Myrange As Range) As Variant
    Dim strInput As String
    Dim regex As Object
    Dim matches As Object
    Set regex = CreateObject("VBScript.RegExp")


    strInput = Myrange.Value

    With regex
        .Pattern = "\d{2}\sPackage_\d{4}-\d{4}"
    End With

    Set matches = regex.Execute(strInput)
    If matches.Count > 0 Then
      textpart = matches(0).Value
    End If

End Function
请注意,要将其作为一个完整的单词进行匹配,您可以添加单词边界:

.Pattern = "\b\d{2}\sPackage_\d{4}-\d{4}\b"
            ^^                          ^^ 
要仅在
\
之后匹配,您可以使用捕获组:

.Pattern = "\\(\d{2}\sPackage_\d{4}-\d{4})\b"
' ...
' and then
' ...
textpart = matches(0).Submatches(0)

您需要使用
\d{2}
来匹配2位区块,而不是
\d{2}
。此外,您正在尝试将整个匹配集合分配给函数结果,同时应提取第一个匹配值并将该值分配给函数结果:

Function textpart(Myrange As Range) As Variant
    Dim strInput As String
    Dim regex As Object
    Dim matches As Object
    Set regex = CreateObject("VBScript.RegExp")


    strInput = Myrange.Value

    With regex
        .Pattern = "\d{2}\sPackage_\d{4}-\d{4}"
    End With

    Set matches = regex.Execute(strInput)
    If matches.Count > 0 Then
      textpart = matches(0).Value
    End If

End Function
请注意,要将其作为一个完整的单词进行匹配,您可以添加单词边界:

.Pattern = "\b\d{2}\sPackage_\d{4}-\d{4}\b"
            ^^                          ^^ 
要仅在
\
之后匹配,您可以使用捕获组:

.Pattern = "\\(\d{2}\sPackage_\d{4}-\d{4})\b"
' ...
' and then
' ...
textpart = matches(0).Submatches(0)

可能“\”被视为转义字符,您必须用反斜杠转义。
02 Package\u 2018-1011
H:\H1801100 MLK中学哈特福德\2-Archive\中不存在!已发布的招标文件包\01文件包\U 2018-0905拆除和减排招标文件集\u图纸-PDF\00 HazMat\HM-1.PDF
。另外,
\D
匹配除数字以外的任何字符,您需要在开始时使用
\D{2}
。可能``被视为转义字符,您必须用反斜杠转义。
02 Package\u 2018-1011
不在
H:\H1801100 MLK中学哈特福德\2-Archive\中!已发布的招标文件包\01文件包\U 2018-0905拆除和减排招标文件集\u图纸-PDF\00 HazMat\HM-1.PDF
。另外,
\D
匹配除数字以外的任何字符,您需要在开始时使用
\D{2}