我正在尝试使用正则表达式匹配任何字符(这只是一个较大项目的一段代码)。我得到了以下工作,但似乎是错误的,有没有一种正确的方法来通过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
. "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/