我需要每周分析、总结和批次几千行文字。Excel通配符不够灵活,我想移除粘贴到Notepad++进行处理或输入脚本的额外步骤。
这是我想出的工具。他们仍然有点慢--也许在公司笔记本电脑上每秒3000行--但它们很方便。
RXMatch --返回第一次匹配,选项返回子组。
=RXMatch("Apple","A(..)",1) -> "pp"RXCount -比赛数
=RXCount("Apple","p") -> 2RXPrint --将第一个匹配和/或子组嵌入到模板字符串中
=RXPrint("Apple","(\S)\S+","\1 is for \0") -> "A is for Apple"RXPrintAll --将每个匹配嵌入到模板字符串中,加入结果
=RXPrintAll("Apple Banana","(\S)\S+","\1 is for \0") -> "A is for Apple, B is for Banana"RXMatches --返回一个垂直的匹配数组,选项返回子组
=RXMatches("Apple Banana","\S+") -> {"Apple";"Banana"}发布于 2020-06-26 19:14:57
在处理UDF时,缓存创建的对象至关重要。
例如:
Public Function RegexTest(ByVal vHaystack As Variant, ByVal sPattern As String, Optional ByVal sFlags As String = "") As Boolean
'If haystack is an error then return false
If IsError(vHaystack) Then Exit Function
'Stringify haystack
Dim sHaystack As String: sHaystack = vHaystack
'Cache regular expressions, especially important for formulae
Static lookup As Object
If lookup Is Nothing Then Set lookup = CreateObject("Scripting.Dictionary")
'If cached object doesn't exist, create it
Dim sKey As String: sKey = sPattern & "-" & sFlags
If Not lookup.exists(sKey) Then
'Create regex object
Set lookup(sKey) = CreateObject("VBScript.Regexp")
'Bind flags
For i = 1 To Len(sFlags)
Select Case Mid(sFlags, i, 1)
Case "i"
lookup(sKey).IgnoreCase = True
Case "g"
lookup(sKey).Global = True
End Select
Next
'Set pattern
lookup(sKey).Pattern = sPattern
End If
'Use test function of regex object
RegexTest = lookup(sKey).test(sHaystack)
End Function将其应用到您自己的函数中,您将看到这极大地提高了大量单元格上的执行速度。
https://stackoverflow.com/questions/19481175
复制相似问题