Section 18.1: 정규 표현식 패턴 단일 매칭

using System.Text.RegularExpressions; string pattern = ":(.*?):"; string lookup = "--:text in here:--"; // Regex 객체를 생성하면서 매칭에 사용할 정규 표현식 패턴을 파라미터로 전달한다 Regex rgxLookup = new Regex(pattern, RegexOptions.Singleline, TimeSpan.FromSeconds(1)); // Regex 객체로부터 매칭 결과를 받아온다 Match mLookup = rgxLookup.Match(lookup); // Groups[0] 에는 패턴에 매치된 전체 문자열이 들어있다. // 소괄호 (parentheses) 로 둘러싸여진 매칭 결과들은 배열 인덱스 1 이상의 값을 사용하여 접근할 수 있다. string found = mLookup.Groups[1].Value;

결과:

found = "text in here"
본 문서는 C# Notes for Professionals (라이센스:CC-BY-SA) 를 한글로 번역한 문서입니다. 번역상 오류가 있을 수 있으므로 정확한 내용은 원본 문서를 참고하세요.

[출처] https://books.goalkicker.com/CSharpBook/

반응형

+ Recent posts