How do you execute a regular expression in Excel?

*爱你&永不变心* 提交于 2020-02-10 12:39:56

问题


I am trying to use the following expression to locate a pattern of text in my Excel data. The goal is to then remove the text once it is located.

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

Help!


回答1:


I have made a User Defined Function to run a regex search and display the final match in the cell.

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

You need to open the Visual Basic editor and put the following code into a Module:

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

Also don't forget to add the Reference for Microsoft VBScript Regular Expressions 5.5




回答2:


You didn't specify it but I assumed it was using a VBA macro. i don't think you can do regular expression directly in the sheet using formula.

The following link should help you with regular expression and VBA:

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

Just be sure to add the correct reference "Microsoft VBScript Regular Expressions 5.5"

Hope this help



来源:https://stackoverflow.com/questions/9744602/how-do-you-execute-a-regular-expression-in-excel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!