预设
alt+F11,工具->引用,找到Microsoft VBScript Regular Expressions 5.5
启用
如何初始化正则对象
Dim regx As New RegExp
示例1:根据指定的pattern返货匹配结果
这里抄下这未兄弟的代码 http://stackoverflow.com/a/43128681,增加了一个分组选项。 这个函数可以接收两个参数,文本单元格和pattern单元格,以及一个可选参数,返回第一个匹配的结果,如果没有匹配,则返回not mattched
。第三个参数是为了应对分组情况
Function RegxFunc(strInput As String, regexPattern As String, Optional ByVal sub_match As Integer = 0) As String
Dim regEx As New RegExp
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = regexPattern
End With
If regEx.Test(strInput) Then
Set matches = regEx.Execute(strInput)
If sub_match = 0 Then '如果没有启用分组,则直接返回匹配内容
RegxFunc = matches(0).Value
Else '如果启用了分组
RegxFunc = matches(0).submatches(0)
End If
Else
RegxFunc = "not matched"
End If
End Function
oschina好像没办法识别vb的注释,下面上演示下上面的公式的效果