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/
반응형
'번역 > C# Notes for Professionals' 카테고리의 다른 글
19.2: DateTime.AddDays(Double) 메소드 (0) | 2021.03.17 |
---|---|
19.1: 날짜/시간 (DateTime) 서식 지정하기 (0) | 2021.03.16 |
18.1: 정규 표현식 패턴 단일 매칭 (0) | 2021.03.12 |
18: Regex 를 통한 파싱 (parsing) (0) | 2021.03.11 |
17.2: StringBuilder 를 이용하여 많은 수의 데이터 항목으로부터 문자열을 생성하는 예제 (0) | 2021.03.11 |