简体   繁体   English

Excel中的表达式语法?

[英]Expression syntax in Excel?

I'm struggling with validating a regex syntax that I'd like to use in Excel (VBA). 我正在努力验证要在Excel(VBA)中使用的正则表达式语法。 The syntax runs good in every validator engine on the net but I can't get it to work in Excel. 该语法在网络上的每个验证器引擎中都运行良好,但是我无法在Excel中使用它。

Could anyone help me with this and I'd most grateful. 谁能帮助我,我将不胜感激。

The expression: 表达方式:

    ^.+(?<!/)(?=/?[RP]\d) 

Data to validate: ABC12345/67/R1A 验证数据:ABC12345 / 67 / R1A

Expected result: ABC12345/67 预期结果:ABC12345 / 67

Please check this regex ^.+(?<!/)(?=/?[RP]\\d) as its not giving the expected output. 请检查此正则表达式^.+(?<!/)(?=/?[RP]\\d)因为它没有提供预期的输出。 It works with ^.+(?=/[RP]\\d) . 它适用于^.+(?=/[RP]\\d) Below is sample code. 下面是示例代码。

Sub Main()

    Dim stringToValidate As String
    Dim stringResult As String


    stringToValidate = "ABC12345/67/R1A"
    stringResult = getData(stringToValidate)


End Sub

Function getData(ByVal str As String) As String
    Dim objRegEx As Object
    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.IgnoreCase = True
    objRegEx.Global = True

    objRegEx.Pattern = "^.+(?=/[RP]\d)"

    Set allMatches = objRegEx.Execute(str)

    For i = 0 To allMatches.Count - 1
        result = result & allMatches.Item(i)
    Next

    getData = result
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM