命名捕获组

当前位置:首页>正则>命名捕获组

命名捕获组

Sub 命名捕获组()
    '先复习命名捕获组语法
    ' `(?<name>...)` - Perl命名捕获组
    ' `(?'name'...)` - Perl另一种命名捕获组
    ' `(?P<name>...)` - Python命名捕获组
    Debug.Print "========== Test 1: 命名捕获组 =========="
    
    Dim re As Object
    Set re = CreateObject("loquat.RegExp")
    
    re.Pattern = "(?<year>\d{4})-(?'month'\d{2})-(?P<day>\d{2})"
    
    Dim match As Object
    Set match = re.Execute("Date: 2024-01-19")
    
    If match.Count > 0 Then
        Debug.Print "  Match: " & match(0).Value
        Debug.Print "  Year: " & match(0).SubMatches("year").Value   ' year
        Debug.Print "  Month: " & match(0).SubMatches(1).Value  ' month
        Debug.Print "  Day: " & match(0).SubMatches("day").Value    ' day
    End If
End Sub