【问题标题】:How do you execute a regular expression in Excel?如何在 Excel 中执行正则表达式?
【发布时间】:2012-03-16 21:13:35
【问题描述】:

我正在尝试使用以下表达式在我的 Excel 数据中定位文本模式。目标是在找到文本后将其删除。

/.([0-9]+[]?x[]?[0-9]+[]?dpi)./i

救命!

【问题讨论】:

  • 欢迎来到 Stack Overflow。建议您展示您迄今为止所做的尝试——具体问题往往会得到最多的关注。你能举个例子吗?

标签: excel-2007


【解决方案1】:

我创建了一个用户定义函数来运行正则表达式搜索并在单元格中显示最终匹配项。

=udfRegEx([Cell you want to find the expression],[Cell with the regular expression you want to use])

您需要打开 Visual Basic 编辑器并将以下代码放入模块中:

Function udfRegEx(CellLocation As Range, RegPattern As String)

Dim RegEx As Object, RegMatchCollection As Object, RegMatch As Object
Dim OutPutStr As String

    Set RegEx = CreateObject("vbscript.regexp")
    With RegEx
        .Global = True
        .Pattern = RegPattern
    End With

        OutPutStr = ""
        Set RegMatchCollection = RegEx.Execute(CellLocation.Value)
        For Each RegMatch In RegMatchCollection
            OutPutStr = OutPutStr & RegMatch
        Next
        udfRegEx = OutPutStr

    Set RegMatchCollection = Nothing
    Set RegEx = Nothing
    Set Myrange = Nothing

End Function

别忘了添加 Microsoft VBScript Regular Expressions 5.5 的参考

【讨论】:

  • 很棒的功能,但你对“i”的定义有理由吗?它使该功能在 excel 2013 中对我不起作用,我看不到它在任何地方使用的价值。不过,将其注释掉似乎可以解决问题。
  • 我已将它定义为在函数中使用,但看起来我的函数已被编辑。不再需要“我”
【解决方案2】:

您没有指定它,但我认为它使用的是 VBA 宏。我不认为您可以使用公式直接在工作表中进行正则表达式。

以下链接应该可以帮助您了解正则表达式和 VBA:

http://www.regular-expressions.info/vb.html

请务必添加正确的参考“Microsoft VBScript 正则表达式 5.5”

希望有帮助

【讨论】:

    猜你喜欢
    • 2021-12-09
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 2021-03-06
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    相关资源
    最近更新 更多