산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> C 언어

C# - 정규식

정규 표현식 입력 텍스트와 일치할 수 있는 패턴입니다. .Net 프레임워크는 이러한 일치를 허용하는 정규식 엔진을 제공합니다. 패턴은 하나 이상의 문자 리터럴, 연산자 또는 구성으로 구성됩니다.

정규 표현식 정의를 위한 구성

정규식을 정의할 수 있는 다양한 범주의 문자, 연산자 및 구문이 있습니다. 이러한 구성을 찾으려면 다음 링크를 클릭하십시오.

정규식 클래스

Regex 클래스는 정규식을 나타내는 데 사용됩니다. 다음과 같은 일반적으로 사용되는 방법이 있습니다 -

시니어 번호 방법 및 설명
1

공개 bool IsMatch(문자열 입력)

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

2

public bool IsMatch(문자열 입력, int startat)

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

3

공개 정적 bool IsMatch(문자열 입력, 문자열 패턴)

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

4

공개 MatchCollection 일치(문자열 입력)

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

5

공개 문자열 바꾸기(문자열 입력, 문자열 대체)

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

6

공개 문자열[] 분할(문자열 입력)

Regex 생성자에 지정된 정규식 패턴으로 정의된 위치에서 입력 문자열을 부분 문자열 배열로 분할합니다.

메서드 및 속성의 전체 목록은 C#에 대한 Microsoft 설명서를 참조하십시오.

예시 1

다음 예는 'S'로 시작하는 단어와 일치합니다 -

라이브 데모
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 = "A Thousand Splendid Suns";
         
         Console.WriteLine("Matching words that start with 'S': ");
         showMatch(str, @"\bS\S*");
         Console.ReadKey();
      }
   }
}

위의 코드를 컴파일하고 실행하면 다음과 같은 결과가 생성됩니다. -

Matching words that start with 'S':
The Expression: \bS\S*
Splendid
Suns

예시 2

다음 예는 'm'으로 시작하고 'e'로 끝나는 단어를 찾습니다 -

라이브 데모
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

예시 3

이 예는 여분의 공백을 대체합니다 -

라이브 데모
using System;
using System.Text.RegularExpressions;

namespace RegExApplication {
   class Program {
      static void Main(string[] args) {
         string input = "Hello   World   ";
         string pattern = "\\s+";
         string replacement = " ";
         
         Regex rgx = new Regex(pattern);
         string result = rgx.Replace(input, replacement);

         Console.WriteLine("Original String: {0}", input);
         Console.WriteLine("Replacement String: {0}", result);    
         Console.ReadKey();
      }
   }
}

위의 코드를 컴파일하고 실행하면 다음과 같은 결과가 생성됩니다. -

Original String: Hello World   
Replacement String: Hello World   

C 언어

  1. C# 식, 문 및 블록(예제 포함)
  2. C# 문자열
  3. Java 표현식, 명령문 및 블록
  4. 자바 문자열
  5. 자바 열거형 문자열
  6. 자바 람다 표현식
  7. C++ 문자열:strcpy(), strcat(), strlen(), strcmp() 예
  8. C의 문자열:변수 선언 방법, 초기화, 인쇄, 예제
  9. 예제가 있는 Python 문자열 count()
  10. 자바 - 문자열 클래스