본문 바로가기
Unity3D/Unity & C#

Unity & C# ~ Regex 사용해보기

by 캬캬백곰 2022. 12. 28.
728x90

c#에서 문자열 패턴을 찾거나 문자열의 잘못된 입력 등을 확인할 때 사용할 수 있음.


메타문자

^           첫 글자
$           마지막 글자
\w         문자(영숫자)
\s          공백
\d          숫자
*           0 혹은 그 이상
+          1개 이상
?          0 또는 1
.           새로운 라인을 제외한 한 문자
[ ]         가능한 문자
[^ ]       가능하지 않은 문자
[ - ]       가능한 문자 범위
{n , m}  최소 n개부터 최대 m개까지
(  )        그룹
|            논리 OR


예시

// a-z 범위의 문자
string match = @"[a-z]";

// '강'으로 시작하고 '구'로 끝나는 문자
string match = @"^강\w*구$";

// 2자리 숫자
string match = @"\d{2}";

// 핸드폰 번호 ex) 000-0000-0000
string match = @"(\d{3}-\d{4}-\d{4})";

 

- 참조

https://learn.microsoft.com/ko-kr/dotnet/api/system.text.regularexpressions.regex?view=net-7.0

 

Regex 클래스 (System.Text.RegularExpressions)

변경할 수 없는 정규식을 나타냅니다.

learn.microsoft.com

 

https://regexr.com/

 

RegExr: Learn, Build, & Test RegEx

RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).

regexr.com

 

728x90
반응형