티스토리 뷰

JAVA

(JAVA) 자바 정규표현식 Regular Expression

가독성 0% 2017. 2. 12. 04:07
반응형

문자열을 검증하는 방법으로 사용한다.


[ ] 

한개의 문자 

\d 

한 개의 숫자 [0-9]와 동일 

\s

공백 

\w

한 개의 알파벳 또는 한 개의 숫자 

 ?

없음 또는 한개 

없음 또는 한개 이상 

한개 이상 

{n} 

정확히 n개 

{n,} 

최소한 n개 

{n,m} 

n개부터 m개까지 

( ) 

그룹핑 


public class zzzzz {
	public static void main(String[] args) {
		
		//정구 표현식 문자열 검증
		boolean result = Pattern.matches("[abc]", "a"); //true
		boolean result2 = Pattern.matches("[abc]", "e"); //false
		
		//abc 이외의 하나의 문자
		boolean result3 = Pattern.matches("[^abc]", "a"); //false
		boolean result4 = Pattern.matches("[^abc]", "d"); //true
		
		//a~z 중의 하나의 문자
		boolean result5 = Pattern.matches("[a-z]", "a"); //true
		boolean result6 = Pattern.matches("[a-z]", "A"); //false
		
		//전화번호 검증
		boolean result7 = Pattern.matches("(019)-\\d{3}-\\d{4}", "019-111-1111"); //true
		
		//이메일 검증
		boolean result8 = Pattern.matches("\\w+@\\w+.(com)", "java2017@gmail.com"); //true
	}
}
반응형
댓글
최근에 올라온 글
최근에 달린 댓글