C# WPF에서 정규 표현식을 사용하는 기본적인 방법을 설명하겠습니다.
1. 네임스페이스 추가
정규 표현식을 사용하기 위해 System.Text.RegularExpressions 네임스페이스를 추가해야 합니다.
using System.Text.RegularExpressions;
2. 정규 표현식 클래스
C#에서는 Regex 클래스를 사용하여 정규 표현식을 처리합니다.
3. 주요 메서드
IsMatch: 패턴과 일치하는지 확인합니다.
Match: 첫 번째 일치를 찾아 반환합니다.
Matches: 모든 일치를 찾아 반환합니다.
Replace: 일치하는 부분을 다른 문자열로 교체합니다.
Split: 패턴을 기준으로 문자열을 분할합니다.
4. 사용 예시
1. IsMatch 예제
문자열이 특정 패턴과 일치하는지 확인합니다.
string input = "hello123";
string pattern = @"\d+"; // 숫자를 찾는 패턴
bool isMatch = Regex.IsMatch(input, pattern);
Console.WriteLine(isMatch); // true
string input = "hello123";
string pattern = @"\d+";
Match match = Regex.Match(input, pattern);
if (match.Success)
{
Console.WriteLine($"Found: {match.Value}"); // Found: 123
}
string input = "abc123def456";
string pattern = @"\d+";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match m in matches)
{
Console.WriteLine($"Found: {m.Value}"); // Found: 123, Found: 456
}
string input = "hello123world456";
string pattern = @"\d+";
string replacement = "#";
string result = Regex.Replace(input, pattern, replacement);
Console.WriteLine(result); // hello#world#
string input = "one,two;three four";
string pattern = @"[,\s;]+"; // 쉼표, 공백 또는 세미콜론으로 분할
string[] result = Regex.Split(input, pattern);
foreach (string s in result)
{
Console.WriteLine(s); // one, two, three, four
}
6.C# WPF에서 사용할 수 있는 정규 표현식에 대한 기본적인 내용
