>  기사  >  백엔드 개발  >  C#의 Regex 클래스와 해당 클래스 메서드는 무엇입니까?

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

王林
王林앞으로
2023-08-31 10:45:101050검색

C# 中的 Regex 类及其类方法是什么?

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

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

교사 ID 메서드 및 설명
1 public bool IsMatch(문자열 입력)

여부를 나타냅니다. 정규식을 지정하려면 표현식 Reg ex 구문 이 함수는 지정된 입력 문자열에서 일치하는 항목을 찾습니다.

2 public bool IsMatch(string input, int startat)

Regex 생성자에 지정된 정규식이 발견된 지정된 입력 문자열 일치 항목의 문자열에 지정된 시작 위치에서 시작하는지 여부를 나타냅니다.

3 public static bool IsMatch(String input, String Pattern)

지정된 정규식이 지정된 입력 문자열에서 일치 항목을 찾았는지 여부를 나타냅니다.

4 public MatchCollection 일치(문자열 입력)

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

td>

5 공개 문자열 대체(문자열 입력, 문자열 대체)

지정된 입력 문자열에서 정규식 패턴과 일치하는 모든 문자열을 지정된 대체 문자열로 바꿉니다.

6 public string[] Split(string input)

입력 문자열을 하위 문자열 배열로 분할합니다. 해당 위치는 Regex 생성자에 지정된 정규식 패턴에 의해 정의됩니다.

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

Example

Live Demonstration

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();
      }
   }
}

Output

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

위 내용은 C#의 Regex 클래스와 해당 클래스 메서드는 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제