问题
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