RegularExpressions 네임스페이스에서 Culture를 구분하지 않는 작업 수행

업데이트: 2007년 11월

System.Text.RegularExpressions 네임스페이스의 메서드는 Thread.CurrentCulture 속성을 사용하여 대/소문자를 구분하지 않고 지정된 작업을 수행합니다. 따라서 RegularExpressions 네임스페이스의 대/소문자 비구분 작업은 기본적으로 문화권을 구분합니다. 예를 들어, Regex 클래스options 매개 변수를 지정하여 새 인스턴스를 초기화할 수 있는 생성자를 제공합니다. options 매개 변수는 RegexOptions 열거형 값의 비트 OR 조합입니다. RegexOptions 열거형에는 대/소문자를 구분하지 않고 찾도록 지정하는 IgnoreCase 멤버가 포함됩니다. IgnoreCaseoptions 매개 변수의 일부로 Regex 생성자에 전달할 경우 대/소문자를 구분하지 않으며 문화권을 구분하는 다른 검색이 수행됩니다.

RegularExpressions 네임스페이스의 메서드에서 문화권을 구분하지 않는 동작을 얻으려면 RegexOptions 열거형의 CultureInvariant 멤버를 options 매개 변수의 일부로 Regex 생성자에 전달합니다. 다음 예제에서는 대/소문자를 구분하지 않으며 문화권을 구분하지 않는 Regex를 만드는 방법을 보여 줍니다.

Imports System
Imports System.Globalization
Imports System.Text.RegularExpressions

Public Class CultureChange
    Public Shared Sub Main()
      ' Perform a case-insensitive, culture-insensitive
      ' Regex operation.
      Dim TestString As String = "i"
      Dim r As New Regex("I", RegexOptions.IgnoreCase Or _
                   RegexOptions.CultureInvariant)
      Dim result As Boolean = r.IsMatch(TestString)
      Console.WriteLine("The result of the comparison is: {0}", result)
   End Sub
End Class
using System;
using System.Globalization;
using System.Text.RegularExpressions;

public class CultureChange
{
   public static void Main() 
   {
      // Perform the case-insensitive, culture-insensitive 
      // Regex operation.
      String TestString = "i";
      Regex r = new Regex("I", RegexOptions.IgnoreCase | 
                RegexOptions.CultureInvariant);
      bool result = r.IsMatch(TestString);
      Console.WriteLine("The result of the comparison is: {0}",
                         result); 
   }
}

이 코드는 문자열 "i"와 "I"의 대/소문자를 구분하지 않는 Regex.IsMatchInvariantCulture에 대해 true를 반환함을 확인하는 다음과 같은 출력을 생성합니다.

The result of the match is: True

참고 항목

참조

System.Text.RegularExpressions

기타 리소스

Culture의 영향을 받지 않는 문자열 작업 수행

정규식 언어 요소