.net 的 vbscript 语法是什么:
\b[a-z]+\b
你好
尝试替换源中的所有字母单词。
.net regex 测试器确认此模式将找到所有单词:
\b[a-z]+\b
这:
发现、行动
返回:
发现
操作
http://regexlib.com/RETester.aspx
但是,在带有 vbscript 对象的 excel vba 中,它会失败:
Sub test()
Dim re As New VBScript_RegExp_55.RegExp
re.Global = True
re.Pattern = "\b[a-z]+\b"
Debug.Print re.Replace("Findings, Actions", "xyz")
Set re = Nothing
End Sub
' 输出与输入相同——不发生替换
答案1
Sub test()
Dim RE As Object
Set RE = CreateObject("VBScript.RegExp")
RE.ignoreCase = True
RE.Global = True
RE.Pattern = "\b[a-z]+\b"
Debug.Print RE.Replace("Findings,Actions", "xyz")
End Sub
输出
XYZ,XYZ
答案2
以下是我如何通过正则表达式替换从单元格中提取数字:
Function REGREP(value As Range, searchString As String, replaceString As String,
Optional ignoreCase As Boolean = True, Optional searchGlobal As Boolean = True)
'Dim reg As New RegExp
Dim reg As Object
Set reg = CreateObject("vbscript.regexp")
reg.ignoreCase = ignoreCase
reg.Global = searchGlobal
reg.Pattern = searchString
REGREP = reg.replace(value.value, replaceString)
End Function
Function ASNUM(value As Range)
ASNUM = REGREP(value, "[^0-9.]+", "")
End Function
'Usage:
REGREP(A1, "[^0-9.]+", "")
ASNUM(A1)