번역/C# Notes for Professionals
18.2: 정규 표현식 패턴 다중 매칭
노초코
2021. 3. 12. 22:39
Section 18.2: 정규 표현식 패턴 다중 매칭
using System.Text.RegularExpressions;
List < string > found = new List < string > ();
string pattern = ":(.*?):";
string lookup = "--:text in here:--:another one:-:third one:---!123:fourth:";
// Regex 객체를 생성하면서 매칭에 사용할 정규 표현식 패턴을 파라미터로 전달한다
Regex rgxLookup = new Regex(pattern, RegexOptions.Singleline, TimeSpan.FromSeconds(1));
MatchCollection mLookup = rgxLookup.Matches(lookup);
foreach(Match match in mLookup) {
found.Add(match.Groups[1].Value);
}
출력 결과:
text in here
another one
third one
fourth
위 출력 결과는 아래 코드로 생성된 found
리스트에 대한 출력 결과와 동일하다.
found = new List<string>() { "text in here", "another one", "third one", "fourth" }
본 문서는 C# Notes for Professionals (라이센스:CC-BY-SA) 를 한글로 번역한 문서입니다. 번역상 오류가 있을 수 있으므로 정확한 내용은 원본 문서를 참고하세요.
[출처] https://books.goalkicker.com/CSharpBook/
반응형