블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2731)
Unity3D (814)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (57)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (51)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday
03-29 07:22

정규식을 이용하여 특정 문자만 얻는 방법을 알아보겠습니다.

 

- Namespace : System.Text.RegularExpressions

- Class : Regex

- Method : 

 

 public static string Replace(
string input,
string pattern,
string replacement
 )

 

 

1. 숫자만 얻기

 - 정규식 : [^0-9]

1
2
3
4
5
6
string str = "Englsh@korea$101299**한글";
 
// 숫자만 0-9
str = Regex.Replace(str, @"[^0-9]""");
 
// 결과 : 101299
cs

2. 영문자만 얻기

 - 정규식 : [^a-zA-Z]

1
2
3
4
5
6
string str = "Englsh@korea$101299**한글";
 
// 영문자 a-z A-Z
str = Regex.Replace(str, @"[^a-zA-Z]""");
 
// 결과 : Englshkorea
cs

 

3. 한글만 얻기

 - 정규식 : [^가-힣]

1
2
3
4
5
6
string str = "Englsh@korea$101299**한글";
 
// 한글만 가-힣
str = Regex.Replace(str, @"[^가-힣]""");
 
// 결과 : 한글
cs

 

4. 특수문자 제거

  - 정규식 : [^0-9a-zA-Z가-힣]

1
2
3
4
5
string str = "Englsh@korea$101299**한글";
// 특수문자 제거
str = Regex.Replace(str, @"[^0-9a-zA-Z가-힣]""");
 
// 결과 : Englshkorea101299한글
cs

 

※ 위의 예에서와 같이 정규식을 잘 이용하면 얻고자 하는 문자를 쉽게 처리 할 수 있습니다.

 

 

[출처]

http://docko.tistory.com/entry/C-%EC%A0%95%EA%B7%9C%EC%8B%9D%EC%9D%84-%EC%9D%B4%EC%9A%A9%ED%95%9C-%EB%AC%B8%EC%9E%90-%EB%B3%80%ED%99%98

반응형
Posted by blueasa
, |