Hi
Not too good with regular expressions, I have below which returns "15th"
Ideally I would like it to just return the th, nd, st part
ResultX = RegExSearch("15th December", "\b(\d+)(st|nd|rd|th)\b")
I could simply use Right(ResultX,2) but did just think this is probably done in the pattern, 1 less command
Thanks I/A
------------------------------------------------------
I copied from elsewhere but seems standard'ish
------------------------------------------------------
Public Function RegExSearch(ByVal text As String, ByVal extract_what As String, Optional seperator As String = "") As String
' https://stackoverflow.com/questions/8146485/returning-a-regex-match-in-vba-excel ' I renamed
Dim i As Long, j As Long
Dim result As String
Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
RE.pattern = extract_what
RE.Global = True
RE.IgnoreCase = True
Set allMatches = RE.Execute(text)
For i = 0 To allMatches.Count - 1
For j = 0 To allMatches.Item(i).submatches.Count - 1
result = result & seperator & allMatches.Item(i).submatches.Item(j)
Next
Next
If Len(result) <> 0 Then
result = Right(result, Len(result) - Len(seperator))
End If
RegExSearch = result
End Function
Not too good with regular expressions, I have below which returns "15th"
Ideally I would like it to just return the th, nd, st part
ResultX = RegExSearch("15th December", "\b(\d+)(st|nd|rd|th)\b")
I could simply use Right(ResultX,2) but did just think this is probably done in the pattern, 1 less command
Thanks I/A
------------------------------------------------------
I copied from elsewhere but seems standard'ish
------------------------------------------------------
Public Function RegExSearch(ByVal text As String, ByVal extract_what As String, Optional seperator As String = "") As String
' https://stackoverflow.com/questions/8146485/returning-a-regex-match-in-vba-excel ' I renamed
Dim i As Long, j As Long
Dim result As String
Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
RE.pattern = extract_what
RE.Global = True
RE.IgnoreCase = True
Set allMatches = RE.Execute(text)
For i = 0 To allMatches.Count - 1
For j = 0 To allMatches.Item(i).submatches.Count - 1
result = result & seperator & allMatches.Item(i).submatches.Item(j)
Next
Next
If Len(result) <> 0 Then
result = Right(result, Len(result) - Len(seperator))
End If
RegExSearch = result
End Function