在VBA中使用带有RegEx的通配符(*)来匹配任何内容

问题描述 投票:0回答:1

我正在尝试使用正则表达式匹配任何字符(这只是一个较大项目的一段代码)。我得到了以下工作,但似乎是错误的,有没有一种正确的方法来通过RegEx搜索任何角色?

strPattern = "([!@#$%^&*()]?[a-z]?[0-9]?)"

例如:MCVE

Public Sub RegExSearch()
    Dim regexp As Object
    Dim rng As Range, rcell As Range
    Dim strInput As String, strPattern As String

    Set regexp = CreateObject("vbscript.regexp")
    Set rng = ActiveSheet.Range("A1:A1")

    With regexp 
    .Global = False 
    .MultiLine = False 
    .ignoreCase = True 
    .Pattern = strPattern 
    End With

    For Each rcell In rng.Cells

        strPattern = "([!@#$%^&*()]?[a-z]?[0-9]?)" ' This matches everything, but seems improper

        If strPattern <> "" Then
            strInput = rcell.Value

            If regexp.test(strInput) Then
                MsgBox rcell & " Matched in Cell" & rcell.Address
            End If
        End If
    Next
End Sub
regex excel vba
1个回答
3
投票
. "Wildcard." The unescaped period matches any character, except a new line. 

strPattern = "."

或者@RonRosenfeld指出,如果你需要匹配所有包含“新线”的东西,那么这将有效。

strPattern = "[/S/s]*" 

https://wellsr.com/vba/2018/excel/vba-regex-regular-expressions-guide/

© www.soinside.com 2019 - 2024. All rights reserved.