본문 바로가기
Regular Expressions(정규표현식)

Matches 컬렉션

2023. 8. 21.

이 글은 도움말을 번역한 글입니다.

MatchCollection

정규식 Match 개체의 컬렉션입니다.

 

비고
Matches 컬렉션에는 개별 Match 개체가 포함되어 있으며 RegExp 개체의 Execute 메서드를 통해서만 만들 수 있습니다. Matches 컬렉션의 Count 속성은 개별 Match 개체 속성과 마찬가지로 읽기 전용입니다.

정규식이 실행되면 0개 이상의 Match 개체가 생성될 수 있습니다. 각 Match 개체는 정규식으로 찾은 문자열, 문자열의 길이 및 일치 항목이 발견된 위치에 대한 인덱스에 대한 액세스를 제공합니다.

다음 코드는 정규식 검색에서 Matches 컬렉션을 가져오는 방법과 컬렉션을 반복하는 방법을 보여줍니다.

Function RegExpTest(patrn As String, strng As String) As String
    Dim regEx As Object
    Dim Match As Object
    Dim Matches As Object
    Dim RetStr As String
    
    Set regEx = CreateObject("VBScript.RegExp")   ' 정규 표현식 개체 생성.
    regEx.pattern = patrn   ' 패턴 설정.
    regEx.IgnoreCase = True   ' 대소문자 무시 설정.
    regEx.Global = True   ' 전체 문자열을 검색하도록 설정합니다.
    Set Matches = regEx.Execute(strng)   ' 검색 실행.
    
    For Each Match In Matches   ' Matches 컬렉션 반복 처리.
        RetStr = RetStr & "일치 위치: "
        RetStr = RetStr & Match.FirstIndex & ". 일치 값: '"
        RetStr = RetStr & Match.Value & "'." & vbCrLf
    Next
    
    RegExpTest = RetStr
End Function

Sub TestRegExp()
    Debug.Print RegExpTest("is.", "IS1 is2 IS3 is4")
End Sub

' Output
' 일치 위치: 0. 일치 값: 'IS1'.
' 일치 위치: 4. 일치 값: 'is2'.
' 일치 위치: 8. 일치 값: 'IS3'.
' 일치 위치: 12. 일치 값: 'is4'.

속성

Count Property

MatchCollection 개체에 포함된 Match 개체의 수를 반환합니다. 읽기 전용 속성입니다.

 

비고

 

다음 코드는 MatchCollection 에서 Count 속성을 사용하는 방법을 보여줍니다.

Function GetMatchCount(inputStr As String) As Long
    Dim regEx As Object
    Dim Matches As Object
    
    Set regEx = CreateObject("VBScript.RegExp")
    regEx.pattern = "is." ' is. 패턴을 찾는 패턴
    regEx.IgnoreCase = True ' 대소문자 무시 설정
    regEx.Global = True ' 전체 문자열을 검색하도록 설정
    
    ' Matches 컬렉션을 가져옴
    Set Matches = regEx.Execute(inputStr)
    
    ' Matches 컬렉션 내의 Match 개체의 수 반환
    GetMatchCount = Matches.count
End Function

Sub TestMatchCollectionCount()
    Dim inputStr As String
    Dim count As Long
    
    ' 입력 문자열 설정
    inputStr = "IS1 is2 IS3 is4"
    
    ' Matches 컬렉션 내의 Match 개체의 수를 얻음
    count = GetMatchCount(inputStr)
    
    ' 결과 출력
    MsgBox "입력 문자열: " & inputStr & vbCrLf & "일치하는 패턴의 수: " & count
End Sub

' Output
' 입력 문자열: IS1 is2 IS3 is4
' 일치하는 패턴의 수: 4

Item Property

컬렉션 내에서 지정한 인덱스에 해당하는 Match 개체를 반환합니다. Item 속성을 사용하여 컬렉션 내의 특정 Match 개체에 접근할 수 있습니다.

 

비고

다음 코드는 MatchCollection 에서 Item 속성을 사용하는 방법을 보여줍니다.

Function GetFirstMatchDetails(inputStr As String) As String
    Dim regEx As Object
    Dim Matches As Object
    Dim targetMatch As Object
    Dim matchDetails As String
    
    Set regEx = CreateObject("VBScript.RegExp")
    regEx.pattern = "is." ' is. 패턴을 찾는 패턴
    regEx.IgnoreCase = True ' 대소문자 무시 설정
    regEx.Global = True ' 전체 문자열을 검색하도록 설정
    
    ' Matches 컬렉션을 가져옴
    Set Matches = regEx.Execute(inputStr)
    
    ' 첫 번째 Match 개체에 접근
    Set targetMatch = Matches.Item(0)
    
    ' Match 개체의 세부 정보 생성
    matchDetails = "일치 값: '" & targetMatch.Value & "'" & vbCrLf
    matchDetails = matchDetails & "일치 시작 위치: " & targetMatch.FirstIndex & vbCrLf
    matchDetails = matchDetails & "일치 길이: " & targetMatch.Length
    
    ' Match 개체의 세부 정보 반환
    GetFirstMatchDetails = matchDetails
End Function

Sub TestMatchCollectionDetails()
    Dim inputStr As String
    Dim matchInfo As String
    
    ' 입력 문자열 설정
    inputStr = "IS1 is2 IS3 is4"
    
    ' 첫 번째 Match 개체의 세부 정보를 얻음
    matchInfo = GetFirstMatchDetails(inputStr)
    
    ' 결과 출력
    Debug.Print "입력 문자열: " & inputStr & vbCrLf & matchInfo
End Sub

' Output
' 입력 문자열: IS1 is2 IS3 is4
' 일치 값: 'IS1'
' 일치 시작 위치: 0
' 일치 길이: 3

 

도움말 출처

 

Matches Collection

Table of contents Matches Collection Article 07/17/2015 In this article --> Collection of regular expression Match objects. A Matches collection contains individual Match objects, and can be only created using the Execute method of the RegExp object. The C

learn.microsoft.com

 

'Regular Expressions(정규표현식)' 카테고리의 다른 글

SubMatches 컬렉션  (0) 2023.08.21
Match 개체  (0) 2023.08.20
RegExp 개체  (0) 2023.08.20
정규 표현식 소개  (0) 2023.08.18