Ever wanted to extract a sub string from a cell in Microsoft Excel using a Regular expression? Here’s a Visual Basic snippet that will do just that.
Function getRegexGroup(inRegex As String, inValue As String, inGroup As Integer) Set myRegExp = CreateObject("vbscript.regexp") myRegExp.IgnoreCase = True myRegExp.Global = True myRegExp.Pattern = inRegex Set myMatches = myRegExp.Execute(inValue) Set SubMatches = myMatches.Item(0).SubMatches getRegexGroup = SubMatches.Item(inGroup) End Function |
You can use it like so:
Image may be NSFW.
Clik here to view.
=getRegexGroup([REGULAR EXPRESSION], [INPUT STRING], [GROUP NUMBER TO EXTRACT])