Regex.Matches 메서드

정의

입력 문자열에 있는 정규식을 모두 검색하고 일치 항목을 모두 반환합니다.

오버로드

Matches(String, String, RegexOptions, TimeSpan)

지정된 일치 옵션 및 제한 시간 간격을 사용하여 지정된 입력 문자열에서 지정된 정규식을 모두 검색합니다.

Matches(String, String, RegexOptions)

지정된 일치 옵션을 사용하여 지정된 입력 문자열에서 지정된 정규식을 모두 검색합니다.

Matches(String, Int32)

문자열의 지정된 시작 위치에서 시작하여 지정된 입력 문자열에 있는 정규식을 모두 검색합니다.

Matches(String)

지정된 입력 문자열에 있는 정규식을 모두 검색합니다.

Matches(String, String)

지정된 입력 문자열에서 지정된 정규식을 모두 검색합니다.

Matches(String, String, RegexOptions, TimeSpan)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

지정된 일치 옵션 및 제한 시간 간격을 사용하여 지정된 입력 문자열에서 지정된 정규식을 모두 검색합니다.

public:
 static System::Text::RegularExpressions::MatchCollection ^ Matches(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options, TimeSpan matchTimeout);
public static System.Text.RegularExpressions.MatchCollection Matches (string input, string pattern, System.Text.RegularExpressions.RegexOptions options, TimeSpan matchTimeout);
static member Matches : string * string * System.Text.RegularExpressions.RegexOptions * TimeSpan -> System.Text.RegularExpressions.MatchCollection
Public Shared Function Matches (input As String, pattern As String, options As RegexOptions, matchTimeout As TimeSpan) As MatchCollection

매개 변수

input
String

일치 항목을 검색할 문자열입니다.

pattern
String

일치 항목을 찾을 정규식 패턴입니다.

options
RegexOptions

일치 옵션을 지정하는 열거형 값의 비트 조합입니다.

matchTimeout
TimeSpan

시간 제한 간격이거나, 메서드에 시간 제한이 없어야 함을 나타내는 InfiniteMatchTimeout 입니다.

반환

검색에서 찾은 Match 개체의 컬렉션입니다. 일치 항목이 없으면 메서드가 빈 컬렉션 개체를 반환합니다.

예외

정규식 구문 분석 오류가 발생했습니다.

input 또는 patternnull인 경우

optionsRegexOptions 값의 유효한 비트 조합이 아닙니다.

또는

matchTimeout 이 음수, 0 또는 약 24일보다 큽니다.

예제

다음 예제에서는 메서드를 Matches(String, String, RegexOptions, TimeSpan) 호출하여 "es"로 끝나는 문장의 단어와 일치하는 대/소문자 구분 비교를 수행합니다. 그런 다음 메서드를 Matches(String, String, RegexOptions, TimeSpan) 호출하여 입력 문자열과 패턴의 대/소문자를 구분하지 않는 비교를 수행합니다. 두 경우 모두 제한 시간 간격은 1초로 설정됩니다. 출력에서 보여 주듯이 두 메서드는 서로 다른 결과를 반환합니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b\w+es\b";
      string sentence = "NOTES: Any notes or comments are optional.";
      
      // Call Matches method without specifying any options.
      try {
         foreach (Match match in Regex.Matches(sentence, pattern,
                                               RegexOptions.None,
                                               TimeSpan.FromSeconds(1)))
            Console.WriteLine("Found '{0}' at position {1}", 
                              match.Value, match.Index);
      }
      catch (RegexMatchTimeoutException) {
         // Do Nothing: Assume that timeout represents no match.
      }
      Console.WriteLine();

      // Call Matches method for case-insensitive matching.
      try { 
         foreach (Match match in Regex.Matches(sentence, pattern, RegexOptions.IgnoreCase))
            Console.WriteLine("Found '{0}' at position {1}", 
                              match.Value, match.Index);
      }
      catch (RegexMatchTimeoutException) {}
   }
}
// The example displays the following output:
//       Found 'notes' at position 11
//       
//       Found 'NOTES' at position 0
//       Found 'notes' at position 11
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b\w+es\b"
      Dim sentence As String = "NOTES: Any notes or comments are optional."
      
      ' Call Matches method without specifying any options.
      For Each match As Match In Regex.Matches(sentence, pattern, 
                                               RegexOptions.None, 
                                               TimeSpan.FromSeconds(1))
         Try
            Console.WriteLine("Found '{0}' at position {1}", match.Value, match.Index)
         Catch e As RegexMatchTimeoutException
            ' Do Nothing: Assume that timeout represents no match.
         End Try
      Next
      Console.WriteLine()
      
      ' Call Matches method for case-insensitive matching.
      Try
         For Each match As Match In Regex.Matches(sentence, pattern, 
                                                  RegexOptions.IgnoreCase,
                                                  TimeSpan.FromSeconds(1))
            Console.WriteLine("Found '{0}' at position {1}", match.Value, match.Index)
         Next
      Catch de As RegexMatchTimeoutException
         ' Do Nothing: Assume that timeout represents no match.
      End Try
   End Sub
End Module
' The example displays the following output:
'       Found 'notes' at position 11
'       
'       Found 'NOTES' at position 0
'       Found 'notes' at position 11

정규식 패턴 \b\w+es\b 는 다음 테이블과 같이 정의됩니다.

무늬 설명
\b 단어 경계에서 일치 항목 찾기를 시작합니다.
\w+ 하나 이상의 단어 문자를 찾습니다.
es 리터럴 문자열 "es"를 찾습니다.
\b 단어 경계에서 일치 항목 찾기를 끝냅니다.

설명

메서드는 Matches(String, String, RegexOptions, TimeSpan) 단일 일치 항목 대신 입력 문자열에 있는 모든 일치 항목에 대한 정보를 반환한다는 점을 제외하고 메서드와 비슷합니다 Match(String, String, RegexOptions, TimeSpan) . 다음 코드와 동일합니다.

   try {
      Match match = Regex.Match(input, pattern, options,
                                TimeSpan.FromSeconds(1));
      while (match.Success) {
            // Handle match here...

            match = match.NextMatch();
      }  
   }
   catch (RegexMatchTimeoutException) {
      // Do nothing: assume that exception represents no match.
   }
   Try
      Dim match As Match = Regex.Match(input, pattern, options, 
                                       TimeSpan.FromSeconds(1))
      Do While match.Success
            ' Handle match here...

            match = match.NextMatch()
      Loop  
   Catch e As RegexMatchTimeoutException
      ' Do nothing: assume that exception represents no match.
   End Try

정적 Matches 메서드는 지정된 정규식 패턴을 사용하여 개체를 Regex 생성하고 instance 메서드 Matches를 호출하는 것과 같습니다.

매개 변수는 pattern 일치시킬 문자열을 상징적으로 설명하는 정규식 언어 요소로 구성됩니다. 정규식에 대한 자세한 내용은 .NET 정규식정규식 언어 - 빠른 참조를 참조하세요.

메서드는 Matches 지연 계산을 사용하여 반환 MatchCollection 된 개체를 채웁습니다. 와 MatchCollection.CopyTo 같은 MatchCollection.Count 이 컬렉션의 멤버에 액세스하면 컬렉션이 즉시 채워집니다. 지연 평가를 활용하려면 C# 및 For EachVisual Basic에서 와Next 같은 foreach 구문을 사용하여 컬렉션을 반복해야 합니다.

지연 계산으로 인해 메서드를 호출해 Matches 도 예외가 RegexMatchTimeoutException throw되지 않습니다. 그러나 일치하는 작업이 매개 변수에 지정된 이 제한 시간 간격을 초과하는 경우 이 메서드에서 반환된 개체에 대해 MatchCollection 작업을 수행할 때 예외가matchTimeout throw됩니다.

호출자 참고

매개 변수를 matchTimeout 2초와 같은 적절한 값으로 설정하는 것이 좋습니다. 를 지정하여 시간 초과를 InfiniteMatchTimeout사용하지 않도록 설정하면 정규식 엔진이 약간 더 나은 성능을 제공합니다. 그러나 다음 조건에서만 시간 초과를 사용하지 않도록 설정해야 합니다.

  • 정규식에서 처리된 입력이 알려진 신뢰할 수 있는 원본에서 파생되거나 정적 텍스트로 구성된 경우 이렇게 하면 사용자가 동적으로 입력한 텍스트가 제외됩니다.

  • 정규식 패턴을 철저히 테스트하여 일치 항목, 비 일치 항목 및 근거리 일치를 효율적으로 처리하는지 확인합니다.

  • 정규식 패턴에 거의 일치 항목을 처리할 때 과도한 역추적을 유발하는 것으로 알려진 언어 요소가 없는 경우

추가 정보

적용 대상

Matches(String, String, RegexOptions)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

지정된 일치 옵션을 사용하여 지정된 입력 문자열에서 지정된 정규식을 모두 검색합니다.

public:
 static System::Text::RegularExpressions::MatchCollection ^ Matches(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options);
public static System.Text.RegularExpressions.MatchCollection Matches (string input, string pattern, System.Text.RegularExpressions.RegexOptions options);
static member Matches : string * string * System.Text.RegularExpressions.RegexOptions -> System.Text.RegularExpressions.MatchCollection
Public Shared Function Matches (input As String, pattern As String, options As RegexOptions) As MatchCollection

매개 변수

input
String

일치 항목을 검색할 문자열입니다.

pattern
String

일치 항목을 찾을 정규식 패턴입니다.

options
RegexOptions

일치 옵션을 지정하는 열거형 값의 비트 조합입니다.

반환

검색에서 찾은 Match 개체의 컬렉션입니다. 일치 항목이 없으면 메서드가 빈 컬렉션 개체를 반환합니다.

예외

정규식 구문 분석 오류가 발생했습니다.

input 또는 patternnull인 경우

optionsRegexOptions 값의 유효한 비트 조합이 아닙니다.

예제

다음 예제에서는 메서드를 Matches(String, String) 호출하여 "es"로 끝나는 문장에서 단어를 식별한 다음 메서드를 호출 Matches(String, String, RegexOptions) 하여 입력 문자열과 패턴의 대/소문자를 구분하지 않는 비교를 수행합니다. 출력에서 보여 주듯이 두 메서드는 서로 다른 결과를 반환합니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b\w+es\b";
      string sentence = "NOTES: Any notes or comments are optional.";
      
      // Call Matches method without specifying any options.
      foreach (Match match in Regex.Matches(sentence, pattern))
         Console.WriteLine("Found '{0}' at position {1}", 
                           match.Value, match.Index);
      Console.WriteLine();

      // Call Matches method for case-insensitive matching.
      foreach (Match match in Regex.Matches(sentence, pattern, RegexOptions.IgnoreCase))
         Console.WriteLine("Found '{0}' at position {1}", 
                           match.Value, match.Index);
   }
}
// The example displays the following output:
//       Found 'notes' at position 11
//       
//       Found 'NOTES' at position 0
//       Found 'notes' at position 11
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b\w+es\b"
      Dim sentence As String = "NOTES: Any notes or comments are optional."
      
      ' Call Matches method without specifying any options.
      For Each match As Match In Regex.Matches(sentence, pattern)
         Console.WriteLine("Found '{0}' at position {1}", match.Value, match.Index)
      Next
      Console.WriteLine()
      
      ' Call Matches method for case-insensitive matching.
      For Each match As Match In Regex.Matches(sentence, pattern, RegexOptions.IgnoreCase)
         Console.WriteLine("Found '{0}' at position {1}", match.Value, match.Index)
      Next
   End Sub
End Module
' The example displays the following output:
'       Found 'notes' at position 11
'       
'       Found 'NOTES' at position 0
'       Found 'notes' at position 11

정규식 패턴 \b\w+es\b 는 다음 테이블과 같이 정의됩니다.

무늬 설명
\b 단어 경계에서 일치 항목 찾기를 시작합니다.
\w+ 하나 이상의 단어 문자를 찾습니다.
es 리터럴 문자열 "es"를 찾습니다.
\b 단어 경계에서 일치 항목 찾기를 끝냅니다.

설명

메서드는 Matches(String, String, RegexOptions) 단일 일치 항목 대신 입력 문자열에 있는 모든 일치 항목에 대한 정보를 반환한다는 점을 제외하고 메서드와 비슷합니다 Match(String, String, RegexOptions) . 다음 코드와 동일합니다.

Match match = Regex.Match(input, pattern, options);
while (match.Success) {
      // Handle match here...

      match = match.NextMatch();
}
Dim match As Match = Regex.Match(input, pattern, options)
Do While match.Success
      ' Handle match here...

      match = match.NextMatch()
Loop

정적 Matches 메서드는 지정된 정규식 패턴을 사용하여 개체를 Regex 생성하고 instance 메서드 Matches를 호출하는 것과 같습니다.

매개 변수는 pattern 일치시킬 문자열을 상징적으로 설명하는 정규식 언어 요소로 구성됩니다. 정규식에 대한 자세한 내용은 .NET 정규식정규식 언어 - 빠른 참조를 참조하세요.

메서드는 Matches 지연 계산을 사용하여 반환 MatchCollection 된 개체를 채웁습니다. 와 MatchCollection.CopyTo 같은 MatchCollection.Count 이 컬렉션의 멤버에 액세스하면 컬렉션이 즉시 채워집니다. 지연 평가를 활용하려면 C# 및 For EachVisual Basic에서 와Next 같은 foreach 구문을 사용하여 컬렉션을 반복해야 합니다.

지연 계산으로 인해 메서드를 호출해 Matches(String, String) 도 예외가 RegexMatchTimeoutException throw되지 않습니다. 그러나 작업을 수행 하면 예외가 throw 됩니다는 MatchCollection 시간 제한 간격을 현재 애플리케이션 도메인 및 일치 작업의 "REGEX_DEFAULT_MATCH_TIMEOUT" 속성에 의해 정의 된 경우이 메서드에서 반환 된 개체 이 시간 제한 간격을 초과합니다.

호출자 참고

이 메서드는 호출 되는 애플리케이션 도메인의 기본 제한 시간 값에 해당 간격이 지난 후 시간이 합니다. 애플리케이션 도메인에 값에 대 한 제한 시간 값을 정의 되지 않은 경우 InfiniteMatchTimeout, 메서드 시간 초과 방지 하는는 데 사용 됩니다. 여러 패턴 일치를 검색하는 데 권장되는 정적 메서드는 시간 제한 간격을 설정할 수 있는 입니다 Matches(String, String, RegexOptions, TimeSpan).

추가 정보

적용 대상

Matches(String, Int32)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

문자열의 지정된 시작 위치에서 시작하여 지정된 입력 문자열에 있는 정규식을 모두 검색합니다.

public:
 System::Text::RegularExpressions::MatchCollection ^ Matches(System::String ^ input, int startat);
public System.Text.RegularExpressions.MatchCollection Matches (string input, int startat);
member this.Matches : string * int -> System.Text.RegularExpressions.MatchCollection
Public Function Matches (input As String, startat As Integer) As MatchCollection

매개 변수

input
String

일치 항목을 검색할 문자열입니다.

startat
Int32

입력 문자열에서 검색을 시작할 문자 위치입니다.

반환

검색에서 찾은 Match 개체의 컬렉션입니다. 일치 항목이 없으면 메서드가 빈 컬렉션 개체를 반환합니다.

예외

input이(가) null인 경우

startat이 0보다 작거나 input의 길이보다 큰 경우

예제

다음 예제에서는 메서드를 Match(String) 사용하여 "es"로 끝나는 문장에서 첫 번째 단어를 찾은 다음 메서드를 호출 Matches(String, Int32) 하여 "es"로 끝나는 추가 단어를 식별합니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b\w+es\b";
      Regex rgx = new Regex(pattern);
      string sentence = "Who writes these notes and uses our paper?";
      
      // Get the first match.
      Match match = rgx.Match(sentence);
      if (match.Success) {
         Console.WriteLine("Found first 'es' in '{0}' at position {1}", 
                           match.Value, match.Index);
         // Get any additional matches.
         foreach (Match m in rgx.Matches(sentence, match.Index + match.Length))
            Console.WriteLine("Also found '{0}' at position {1}", 
                              m.Value, m.Index);
      }   
   }
}
// The example displays the following output:
//       Found first 'es' in 'writes' at position 4
//       Also found 'notes' at position 17
//       Also found 'uses' at position 27
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b\w+es\b"
      Dim rgx As New Regex(pattern)
      Dim sentence As String = "Who writes these notes and uses our paper?"
      
      ' Get the first match.
      Dim match As Match = rgx.Match(sentence)
      If match.Success Then
         Console.WriteLine("Found first 'es' in '{0}' at position {1}", _
                           match.Value, match.Index)
         ' Get any additional matches.
         For Each match In rgx.Matches(sentence, match.Index + match.Length)
            Console.WriteLine("Also found '{0}' at position {1}", _
                              match.Value, match.Index)
         Next
      End If   
   End Sub
End Module
' The example displays the following output:
'       Found first 'es' in 'writes' at position 4
'       Also found 'notes' at position 17
'       Also found 'uses' at position 27

정규식 패턴 \b\w+es\b 는 다음 테이블과 같이 정의됩니다.

무늬 설명
\b 단어 경계에서 일치 항목 찾기를 시작합니다.
\w+ 하나 이상의 단어 문자를 찾습니다.
es 리터럴 문자열 "es"를 찾습니다.
\b 단어 경계에서 일치 항목 찾기를 끝냅니다.

설명

메서드는 Matches(String, Int32) 단일 일치 항목 대신 입력 문자열에 있는 모든 일치 항목에 대한 정보를 반환한다는 점을 제외하고 메서드와 비슷합니다 Match(String, Int32) . 다음 코드와 동일합니다.

Match match = regex.Match(input, startAt);
while (match.Success) {
      // Handle match here...

      match = match.NextMatch();
}
Dim match As Match = regex.Match(input, startAt)
Do While match.Success
      ' Handle match here...

      match = match.NextMatch()
Loop

메서드가 검색하는 Matches(String, Int32) 정규식 패턴은 클래스 생성자 중 Regex 하나에 대한 호출에 의해 정의됩니다. 정규식 패턴을 형성할 수 있는 요소에 대한 자세한 내용은 정규식 언어 - 빠른 참조를 참조하세요.

에 대한 startat자세한 내용은 의 Match(String, Int32)설명 섹션을 참조하세요.

메서드는 Matches 지연 계산을 사용하여 반환 MatchCollection 된 개체를 채웁습니다. 와 MatchCollection.CopyTo 같은 MatchCollection.Count 이 컬렉션의 멤버에 액세스하면 컬렉션이 즉시 채워집니다. 지연 평가를 활용하려면 C# 및 For EachVisual Basic에서 와Next 같은 foreach 구문을 사용하여 컬렉션을 반복해야 합니다.

지연 계산으로 인해 메서드를 호출해 Matches(String, Int32) 도 예외가 RegexMatchTimeoutException throw되지 않습니다. 그러나 이 메서드에서 반환된 개체에 대해 MatchCollection 작업을 수행할 때 속성이 이 아니 Regex.InfiniteMatchTimeout 고 일치하는 작업이 제한 시간 간격을 초과하는 경우 MatchTimeout 예외가 throw됩니다.

추가 정보

적용 대상

Matches(String)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

지정된 입력 문자열에 있는 정규식을 모두 검색합니다.

public:
 System::Text::RegularExpressions::MatchCollection ^ Matches(System::String ^ input);
public System.Text.RegularExpressions.MatchCollection Matches (string input);
member this.Matches : string -> System.Text.RegularExpressions.MatchCollection
Public Function Matches (input As String) As MatchCollection

매개 변수

input
String

일치 항목을 검색할 문자열입니다.

반환

검색에서 찾은 Match 개체의 컬렉션입니다. 일치 항목이 없으면 메서드가 빈 컬렉션 개체를 반환합니다.

예외

input이(가) null인 경우

예제

다음 예제에서는 메서드를 Matches(String) 사용하여 "es"로 끝나는 문장의 단어를 식별합니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b\w+es\b";
      Regex rgx = new Regex(pattern);
      string sentence = "Who writes these notes?";
      
      foreach (Match match in rgx.Matches(sentence))
         Console.WriteLine("Found '{0}' at position {1}", 
                           match.Value, match.Index);
   }
}
// The example displays the following output:
//       Found 'writes' at position 4
//       Found 'notes' at position 17
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b\w+es\b"
      Dim rgx As New Regex(pattern)
      Dim sentence As String = "Who writes these notes?"
      
      For Each match As Match In rgx.Matches(sentence)
         Console.WriteLine("Found '{0}' at position {1}", match.Value, match.Index)
      Next
   End Sub
End Module
' The example displays the following output:
'       Found 'writes' at position 4
'       Found 'notes' at position 17

정규식 패턴 \b\w+es\b 는 다음 테이블과 같이 정의됩니다.

무늬 설명
\b 단어 경계에서 일치 항목 찾기를 시작합니다.
\w+ 하나 이상의 단어 문자를 찾습니다.
es 리터럴 문자열 "es"를 찾습니다.
\b 단어 경계에서 일치 항목 찾기를 끝냅니다.

설명

메서드는 Matches(String) 단일 일치 항목 대신 입력 문자열에 있는 모든 일치 항목에 대한 정보를 반환한다는 점을 제외하고 메서드와 비슷합니다 Match(String) . 다음 코드와 동일합니다.

Match match = regex.Match(input);
while (match.Success) {
      // Handle match here...

      match = match.NextMatch();
}
Dim match As Match = regex.Match(input)
Do While match.Success
      ' Handle match here...

      match = match.NextMatch()
Loop

컬렉션에는 일치 항목만 포함되며 첫 번째 비매치 시 종료됩니다.

메서드가 검색하는 Matches(String) 정규식 패턴은 클래스 생성자 중 Regex 하나에 대한 호출에 의해 정의됩니다. 정규식 패턴을 형성할 수 있는 요소에 대한 자세한 내용은 정규식 언어 - 빠른 참조를 참조하세요.

메서드는 Matches 지연 계산을 사용하여 반환 MatchCollection 된 개체를 채웁습니다. 와 MatchCollection.CopyTo 같은 MatchCollection.Count 이 컬렉션의 멤버에 액세스하면 컬렉션이 즉시 채워집니다. 지연 평가를 활용하려면 C# 및 For EachVisual Basic에서 와Next 같은 foreach 구문을 사용하여 컬렉션을 반복해야 합니다.

지연 계산으로 인해 메서드를 호출해 Matches(String) 도 예외가 RegexMatchTimeoutException throw되지 않습니다. 그러나 이 메서드에서 반환된 개체에 대해 MatchCollection 작업을 수행할 때 속성이 이 아니 Regex.InfiniteMatchTimeout 고 일치하는 작업이 제한 시간 간격을 초과하는 경우 MatchTimeout 예외가 throw됩니다.

추가 정보

적용 대상

Matches(String, String)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

지정된 입력 문자열에서 지정된 정규식을 모두 검색합니다.

public:
 static System::Text::RegularExpressions::MatchCollection ^ Matches(System::String ^ input, System::String ^ pattern);
public static System.Text.RegularExpressions.MatchCollection Matches (string input, string pattern);
static member Matches : string * string -> System.Text.RegularExpressions.MatchCollection
Public Shared Function Matches (input As String, pattern As String) As MatchCollection

매개 변수

input
String

일치 항목을 검색할 문자열입니다.

pattern
String

일치 항목을 찾을 정규식 패턴입니다.

반환

검색에서 찾은 Match 개체의 컬렉션입니다. 일치 항목이 없으면 메서드가 빈 컬렉션 개체를 반환합니다.

예외

정규식 구문 분석 오류가 발생했습니다.

input 또는 patternnull인 경우

예제

다음 예제에서는 메서드를 Matches(String, String) 사용하여 "es"로 끝나는 문장의 단어를 식별합니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b\w+es\b";
      string sentence = "Who writes these notes?";
      
      foreach (Match match in Regex.Matches(sentence, pattern))
         Console.WriteLine("Found '{0}' at position {1}", 
                           match.Value, match.Index);
   }
}
// The example displays the following output:
//       Found 'writes' at position 4
//       Found 'notes' at position 17
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b\w+es\b"
      Dim sentence As String = "Who writes these notes?"
      For Each match As Match In Regex.Matches(sentence, pattern)
         Console.WriteLine("Found '{0}' at position {1}", match.Value, match.Index)
      Next
   End Sub
End Module
' The example displays the following output:
'       Found 'writes' at position 4
'       Found 'notes' at position 17

정규식 패턴 \b\w+es\b 는 다음 테이블과 같이 정의됩니다.

무늬 설명
\b 단어 경계에서 일치 항목 찾기를 시작합니다.
\w+ 하나 이상의 단어 문자를 찾습니다.
es 리터럴 문자열 "es"를 찾습니다.
\b 단어 경계에서 일치 항목 찾기를 끝냅니다.

설명

메서드는 Matches(String, String) 단일 일치 항목 대신 입력 문자열에 있는 모든 일치 항목에 대한 정보를 반환한다는 점을 제외하고 메서드와 비슷합니다 Match(String, String) . 다음 코드와 동일합니다.

Match match = Regex.Match(input, pattern);
while (match.Success) {
      // Handle match here...

      match = match.NextMatch();
}
Dim match As Match = Regex.Match(input, pattern)
Do While match.Success
      ' Handle match here...

      match = match.NextMatch()
Loop

정적 Matches 메서드는 지정된 정규식 패턴을 사용하여 개체를 Regex 생성하고 instance 메서드 Matches를 호출하는 것과 같습니다.

매개 변수는 pattern 일치시킬 문자열을 상징적으로 설명하는 정규식 언어 요소로 구성됩니다. 정규식에 대한 자세한 내용은 .NET 정규식정규식 언어 - 빠른 참조를 참조하세요.

메서드는 Matches 지연 계산을 사용하여 반환 MatchCollection 된 개체를 채웁습니다. 와 MatchCollection.CopyTo 같은 MatchCollection.Count 이 컬렉션의 멤버에 액세스하면 컬렉션이 즉시 채워집니다. 지연 평가를 활용하려면 C# 및 For EachVisual Basic에서 와Next 같은 foreach 구문을 사용하여 컬렉션을 반복해야 합니다.

지연 계산으로 인해 메서드를 호출해 Matches(String, String) 도 예외가 RegexMatchTimeoutException throw되지 않습니다. 그러나 작업을 수행 하면 예외가 throw 됩니다는 MatchCollection 시간 제한 간격을 현재 애플리케이션 도메인 및 일치 작업의 "REGEX_DEFAULT_MATCH_TIMEOUT" 속성에 의해 정의 된 경우이 메서드에서 반환 된 개체 이 시간 제한 간격을 초과합니다.

호출자 참고

이 메서드는 호출 되는 애플리케이션 도메인의 기본 제한 시간 값에 해당 간격이 지난 후 시간이 합니다. 애플리케이션 도메인에 값에 대 한 제한 시간 값을 정의 되지 않은 경우 InfiniteMatchTimeout, 메서드 시간 초과 방지 하는는 데 사용 됩니다. 여러 패턴 일치를 검색하는 데 권장되는 정적 메서드는 시간 제한 간격을 지정할 수 있는 입니다 Matches(String, String, RegexOptions, TimeSpan).

추가 정보

적용 대상