본문 바로가기
IT/C#

정규식(Regex) 예재

by Andante con moto 2021. 5. 19.
728x90
반응형

정규식은 하나의 언어라고 생각합니다.

때문에 언어를 배우듯 공부를 하거나, 평소 필요할 때마다 구글링으로 해소해야 하고요..

아래 C#에서 사용되는 정규식 사용 예재 구문을 공유합니다.

void RegexTest()
{
    // using System.Text.RegularExpressions.Regex
    Regex rx = new Regex("정규식구문", RegexOptions.Singleline | RegexOptions.IgnoreCase);
    String TargetStr = "정규식 해석할 문장";

    //정규식 결과가 1개일 떄.
    Match mt = rx.Match(TargetStr);
    Console.WriteLine(mt.Value);

    //정규실 결과가 여러개일 때.
    MatchCollection mc = rx.Matches(TargetStr);
    foreach(Match mt1 in mc)
    {
        //만약 key,value 형식이라면 mt1.Groups[""].Value 형태로 값 추출.
        Console.WriteLine(mt1.Value);
    }
}

 

 

728x90
반응형

댓글