특수문자 제거 정규식

Posted by 나에요임마
2018. 6. 21. 01:01 Program/C#

input 문자열에 특수 문자가 있다면 isSpecial 에 true 가 반환된다.

string input = @"특수문자가 있습니까?!@#$%^&~.!";

bool isSpecial = Regex.IsMatch(input, @"[^a-zA-Z0-9가-힣]");

[^a-zA-Z0-9가-힣] 은 앞에 ^ 표시가 붙어 부정을 의미한다.

그러므로 a-zA-Z0-9가-힣 사이에 있는문자가 아닌게 있는지 확인하는 것이다.

 

제거는

input = Regex.Replace(input, @"[^a-zA-Z0-9가-힣]", "", RegexOptions.Singleline);



출처: http://kjcoder.tistory.com/535?category=762633 [kjun.kr]