정규식은 텍스트를 처리하는 강력하고 유연하며 효율적인 방법을 제공합니다. 정규식의 광범위한 패턴 일치 표기법을 사용하면 많은 양의 텍스트를 빠르게 구문 분석하여 다음을 할 수 있습니다.
문자열을 처리하거나 텍스트의 큰 블록을 구문 분석하는 많은 애플리케이션의 경우 정규식은 필수적인 도구입니다.
(Microsoft Document 참조)
using System;
using System.Text.RegularExpressions;
A.B ? ABC, A1B, AAB : AB, AA, AC
A*B ? AAAAB, ACDB : AAA, BA, BBA
A+B ? AB, AAB, AAAB : AC, BB, BA
A?B ? AB, ACB, A1B : A, AAB
A|B ? A, B : AB, AB
[a-z] 소문자 [A-Z] 대문자 [0-9] 숫자 [가-힣] 한글 [\s] 공백 [a-zA-Z가-힣] ...
[a-z]{3} 소문자 3개의 텍스트 [0-9]{1,3} 숫자 1~3자리 [\s]{2,} 2개 이상의 공백
(AB){2} ABAB (AB)* ABABABAB...
IP 주소 : [0-9]{3}\.[0-9]{3}\.[0-9]{3}
string phone_number = "010-1234-5678";
Regex regex = new Regex(@"^01[01678]-[0-9]{4}-[0-9]{4}$");
if (regex.IsMatch(phone_number))
{
Console.WriteLine("Match");
}
else
{
Console.WriteLine("Miss Match");
}
// 결과값 : Match
string ip_address = "내 ip 주소는 123.456.789 입니다";
Regex regex = new Regex(@"^*[0-9]{3}\.[0-9]{3}\.[0-9]{3}$*");
MatchCollection resultColl = regex.Matches(ip_address);
for (int i = 0; i < resultColl.Count; i++)
{
if ((resultColl[i].Groups[0]).ToString().Trim() != "")
{
Console.WriteLine((resultColl[i].Groups[0]).ToString().Trim());
}
}
// 결과값 : 123.456.789
🎈 Microsoft Document : https://docs.microsoft.com/ko-kr/dotnet/standard/base-types/regular-expressions