在Excel单元格中调用正则表达式 [英] Call regex in an Excel cell
本文介绍了在Excel单元格中调用正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用正则表达式匹配从单元格中提取字符串. 我找不到能做到这一点的excel函数,我也不是VBA专家. 我使用Excel 2007.
I want to extract a string from a cell using a regex match. I can't find the excel function that does it and I'm no VBA expert either. I use Excel 2007.
推荐答案
在Excel VBA代码编辑器中,选择工具/参考".在选择器中,检查最新版本的Microsoft VBScript Regular Expresions.
In the Excel VBA code editor select Tools/References. In the picker check the latest version of Microsoft VBScript Regular Expresions.
这是一个简单的用户定义函数,可帮助您入门.如果将其指向其中包含"foobar"或"fooooobar"的单元格,则会返回"foo".
Here is a simple user defined function to get you started. This will return "foo" if you point it at a cell with "foobar" or "fooooobar" in it.
Function RegExtract(contents As String)
Dim regx As RegExp
Set regx = New RegExp
With regx
.Pattern = "f[o]+"
With .Execute(contents)
If .Count Then RegExtract = .Item(0).Value
End With
End With
End Function
这篇关于在Excel单元格中调用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文