Here is the code:
'-----------------------------------------------FRE---------------------------------------
Public Function FRE(StringIn As String, strPattern As String) As String
Dim r As RegExp
Set r = New RegExp
With r
.Global = True
.MultiLine = False
.IgnoreCase = True
.Pattern = strPattern
End With
Set m = r.Execute(StringIn)
FRE = Trim(m(0))
End Function
'------------------------------------------------------------------------------------------------
I named this function FRE - simply because I use it a ton, and didn't want to type some totally expressive name, - FRE is Find using Regular Expression.
There are two parts - the Stringin - which is the string your searching in, and strPattern which is the regular Expression Pattern. If this function finds anything, it's only going to return the first instance. I'm sure we can write better functions for returning arrays, making ranges, etc. This is just dirt simple.
Here is an example of how you could use this function - you have a cell that has a lot of text mixed with phone numbers, but you just want to return the phone numbers from that cell, this function will return the first phone number.
Lets Say A1 = "Joe Smith (456) 456-8456 josmith@google.com"
to return the phone number, the function would look like this FRE(A1,"(\(\d{3}\).*\d{3}.*\d{4})")
And the return would be - (456) 456-8456
Super Simple. I hope this will help somebody out there!
No comments:
Post a Comment