Last active
March 15, 2021 01:28
-
-
Save jca02266/9727dc376cde0bbfdabc3fe2b9da1bf7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function re_match(re As Object, s As String) As Variant | |
Dim ms As Object | |
Set ms = re.Execute(s) | |
Dim arr() As String | |
If ms.Count = 0 Then | |
Exit Function | |
End If | |
Dim m As Object | |
Set m = ms.Item(0) | |
ReDim arr(0) | |
arr(0) = m.Value | |
If m.SubMatches.Count > 0 Then | |
ReDim Preserve arr(m.SubMatches.Count) | |
Dim i As Long | |
For i = 0 To m.SubMatches.Count - 1 | |
arr(i + 1) = m.SubMatches(i) | |
Next | |
End If | |
re_match = arr | |
End Function | |
Sub sample1() | |
Dim re As Object | |
Set re = CreateObject("VBScript.RegExp") | |
re.Global = False | |
re.ignorecase = True | |
re.Pattern = "^(\d+)/(\d+)$" | |
Dim arr As Variant | |
arr = re_match(re, "1/2") | |
If Not IsEmpty(arr) Then | |
Debug.Print arr(1) & "/" & arr(2) | |
End If | |
End Sub | |
Function re_match_global(re As Object, s As String) As Variant | |
Dim ms As Object | |
Set ms = re.Execute(s) | |
Dim arr() As String | |
If ms.Count = 0 Then | |
Exit Function | |
End If | |
ReDim Preserve arr(ms.Count - 1) | |
Dim i As Long | |
For i = 0 To ms.Count - 1 | |
arr(i) = ms.Item(i) | |
Next | |
re_match_global = arr | |
End Function | |
Sub sample2() | |
Dim re As Object | |
Set re = CreateObject("VBScript.RegExp") | |
re.Global = True | |
re.ignorecase = True | |
re.Pattern = "[A-Z]+|[0-9]+" | |
Dim arr As Variant | |
arr = re_match_global(re, "○○町1丁目2番地A103号") | |
If Not IsEmpty(arr) Then | |
Debug.Print Join(arr, "-") | |
End If | |
End Sub | |
Sub sample3() | |
Dim re As Object | |
Set re = CreateObject("VBScript.RegExp") | |
re.Global = True | |
re.ignorecase = True | |
re.Pattern = "[A-Z]+|[0-9]+" | |
Dim match As Variant | |
Set match = re.Execute("○○町1丁目2番地A103号") | |
If match.Count > 0 Then | |
Dim arr() As String | |
ReDim arr(match.Count - 1) | |
Dim i As Long | |
For i = 0 To match.Count - 1 | |
arr(i) = match(i) | |
Next | |
Debug.Print Join(arr, "-") | |
End If | |
End Sub | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment