regex - 在 VBA 中使用通配符 (*) 和正则表达式来匹配任何内容
问题描述
我正在尝试使用正则表达式来匹配任何字符(这只是来自更大项目的一段代码)。我得到了下面的工作,但似乎它是错误的,有没有通过正则表达式搜索任何字符的正确方法?
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/
推荐阅读
- c++ - 插入函数的二叉搜索树问题
- python - Python rfind 包括找到的字符
- c# - 单击相同的按钮但不同的操作 - c#
- django - PythonAnywhere 和 Celery
- c++ - 我对声明和使用字符感到困惑——例如在这个 sscanf 示例中
- android - React Native:java.lang.NoClassDefFoundError:解析失败:Lcom/google/firebase/auth/internal/InternalAuthProvider
- circuit-breaker - Polly 断路器/重试以在网络中断后自动重新启动查询
- linear-programming - 为什么 cvxpy 在这里返回无界?
- sql - 为什么这个 WHERE 子句不返回正确的数据?
- netlogo - NetLogo- 僵尸:如何询问具有特定补丁值的邻居