Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#에서 Regex 클래스와 해당 클래스 메서드는 무엇입니까?

<시간/>

Regex 클래스는 정규식을 나타내는 데 사용됩니다. 정규식은 입력 텍스트와 일치할 수 있는 패턴입니다.

다음은 Regex 클래스의 메소드입니다 -

Sr.No 방법 및 설명
1 공개 bool IsMatch(문자열 입력)
Regex 생성자에 지정된 정규식이 지정된 입력 문자열에서 일치하는 항목을 찾는지 여부를 나타냅니다.
2 public bool IsMatch(문자열 입력, int startat)
Regex 생성자에 지정된 정규식이 문자열의 지정된 시작 위치에서 시작하여 지정된 입력 문자열에서 일치 항목을 찾는지 여부를 나타냅니다.
3 공개 정적 bool IsMatch(문자열 입력, 문자열 패턴)
지정된 정규식이 지정된 입력 문자열에서 일치 항목을 찾는지 여부를 나타냅니다.
4 공개 MatchCollection 일치(문자열 입력)
정규식의 모든 발생에 대해 지정된 입력 문자열을 검색합니다.
5 공개 문자열 바꾸기(문자열 입력, 문자열 대체)
지정된 입력 문자열에서 정규식 패턴과 일치하는 모든 문자열을 지정된 대체 문자열로 바꿉니다.
6 공개 문자열[] 분할(문자열 입력)
Regex 생성자에 지정된 정규식 패턴으로 정의된 위치에서 입력 문자열을 부분 문자열 배열로 분할합니다.

다음 예제에서는 Matches() 메서드를 사용하여 지정된 입력 문자열을 검색합니다. -

예시

using System;
using System.Text.RegularExpressions;

namespace RegExApplication {
   class Program {
      private static void showMatch(string text, string expr) {
         Console.WriteLine("The Expression: " + expr);
         MatchCollection mc = Regex.Matches(text, expr);
         foreach (Match m in mc) {
            Console.WriteLine(m);
         }
      }

      static void Main(string[] args) {
         string str = "make maze and manage to measure it";
         Console.WriteLine("Matching words start with 'm' and ends with 'e':");
         showMatch(str, @"\bm\S*e\b");
         Console.ReadKey();
      }
   }
}

출력

Matching words start with 'm' and ends with 'e':
The Expression: \bm\S*e\b
make
maze
manage
measure