반응형
C# -- 정규식 ; 문자열 추출하기
환경 : windows 7 64bit, visual studio 2013 community
참고 : http://www.csharpstudy.com/Practical/Prac-regex-1.aspx
https://msdn.microsoft.com/ko-kr/library/hs600312.aspx
https://msdn.microsoft.com/ko-kr/library/az24scfc(v=vs.110).aspx
https://msdn.microsoft.com/ko-kr/library/kweb790z(v=vs.110).aspx
<< 미션 >>
아래의 문자열에서 [ ] 내부의 문자열 ' 하하 342' 얻어오기
"테스트 한글[ 하하 342]"
<< 해결책 >>
1. Regex 의 Matches 사용하여, 일치하는 Collection 을 먼저 구한다.
2. 결과 Collection 의 요소 각각에 대해, Match.Groups 를 이용하여, 결과 확인 가능
-- Match.Group[0] ==> 그룹 밖의 정규식 내용 포함한 문자열 결과 ; '[ 하하 342]'
-- Match.Group[1] ==> 그룹만 나타내는 문자열 결과 ; ' 하하 342'
** 소스
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text.RegularExpressions; | |
namespace regexText2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
test1(); | |
test2(); | |
test3(); | |
Console.ReadLine(); | |
} | |
static void test1() | |
{ | |
Console.WriteLine("--------------- test1 -------------------"); | |
String txt = "테스트 한글[ 하하 342]"; | |
Regex reg = new Regex(@"\[(.+)\]"); | |
MatchCollection resultColl = reg.Matches(txt); | |
Console.WriteLine("매치 글자수 == {0}", resultColl.Count); | |
foreach (Match mm in resultColl) | |
{ | |
Group g = mm.Groups[1]; | |
Console.WriteLine(" mm.Groups.Count = {0}", mm.Groups.Count); | |
Console.WriteLine(" mm.Groups[0] = {0}", mm.Groups[0]); | |
Console.WriteLine(" mm.Groups[1] = {0}", mm.Groups[1]); | |
Console.WriteLine("{0}={1}=", g.Index, g.Value); | |
} | |
} | |
// 원하는 것을 제대로 못함. | |
static void test2() | |
{ | |
Console.WriteLine("\n--------------- test2 -------------------"); | |
String txt = "테스트 한글[ 하하 342] [gg ]"; | |
Regex reg = new Regex(@"\[(.+)\]"); | |
MatchCollection resultColl = reg.Matches(txt); | |
Console.WriteLine("매치 글자수 == {0}", resultColl.Count); | |
foreach (Match mm in resultColl) | |
{ | |
Group g = mm.Groups[1]; | |
Console.WriteLine("{0}={1}=", g.Index, g.Value); | |
} | |
} | |
static void test3() | |
{ | |
Console.WriteLine("\n--------------- test3 -------------------"); | |
String txt = "테스트 한글[ 하하 342] [gg ]"; | |
Regex reg = new Regex(@"\[(\s*\w+\s*\w*)\]"); | |
MatchCollection resultColl = reg.Matches(txt); | |
Console.WriteLine("매치 글자수 == {0}", resultColl.Count); | |
foreach (Match mm in resultColl) | |
{ | |
Group g = mm.Groups[1]; | |
Console.WriteLine(" mm.Groups.Count = {0}", mm.Groups.Count); | |
Console.WriteLine(" mm.Groups[0] = {0}", mm.Groups[0]); | |
Console.WriteLine(" mm.Groups[1] = {0}", mm.Groups[1]); | |
Console.WriteLine("{0}={1}=", g.Index, g.Value.Trim()); | |
} | |
} | |
} | |
} |
-- 실행결과
반응형
'C#' 카테고리의 다른 글
C# -- string -> int 변환하기 (0) | 2018.12.27 |
---|---|
c# - TextBox 에서 newline 넣기 (0) | 2018.12.25 |
C# -- event vs delegate ; 작성중... (0) | 2016.04.04 |
C# -- visual studio 2013 에서 코드접기(outline) 설정 (0) | 2016.03.25 |
C# -- List 사용법 (1) | 2016.03.10 |