출처  :  https://blog.naver.com/best8388/150166410183

 정규식 정리 쉬운 설명으로 링크해둠요


<0개 이상의 공백><소문자> pattern은

 "^\\s*[a-z]$"는

 시작공백이0개이상소문자한개

 private static void pmatch(String input){

Pattern pattern = Pattern.compile("^\\s*[a-z]$");

Matcher matcher = pattern.matcher(input);

if(matcher.matches()) System.out.println("true");

else System.out.println("false");

}


<0개 이상의 공백><소문자><1개 이상의 공백><대소문자><숫자> pattern은

 "^\\s*[a-z]\\s+[A-Za-z][0-9]$"는

 시작공백이0개이상소문자한개공백이1개이상대문자나소문자숫자

 private static void pmatch(String input){

Pattern pattern = Pattern.compile("^\\s*[a-z]\\s+[A-Za-z][0-9]$");

Matcher matcher = pattern.matcher(input);

if(matcher.matches()) System.out.println("true");

else System.out.println("false");

}


+정규식

Pattern Class와 Matcher Class를 사용.

 ^

 형식의 시작

 $

 형식의 끝

 \s

 공백

 +*/,.?! 등 기호는 앞에

 \를 붙여줌

 \D

 숫자가 아닌 문자

 \w

 알파벳, 숫자, _(밑줄)

 [0-9]

 숫자 1개

 [a-z]

 소문자 1개

 [A-Z]

 대문자 1개

 [A-Za-z]

 알파벳 대소문자 1개

 [^~~~]

 ~~~을 제외한 것

 .

 모든 범위의 한 문자

 *

 앞의 문자가 0개 이상

 +

 앞의 문자가 1개 이상 반복

 {숫자}

 앞의 문자가 숫자만큼 반복

 |

 또는 의 의미(괄호를 이용해 여러개 가능. ex. (1|2|3))

 [형식]

 안에 있는 형식


http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/package-summary.html



예제

String a와 b가 같은지만 비교 

 a.equals(b)가 boolean 값을 반환

2

String a와 b의 아스키코드 값 비교

 a.compareTo(b)

String a가 아스키코드 값으로 b보다 앞이면  음수 반환.

같으면 0 반환.

String a가 b보다 뒤에 위치한 문자열이면 양수 반환

3

String a안에 abc라는 문자열이 있는지 검색

 a.matches(".*abc.*")

4

String b가 있는지 검색

 a.matches(".*"+b+".*");

 .*는 정규식의 표현 중 하나로,

.은 아무 문자나 기호를 뜻하고

*는 앞에 있는 것이 0개 이상이라는 뜻.



1
2
3
4
5
6
//특수문자 제거 하기
   public static String StringReplace(String str){      
      String match = "[^\uAC00-\uD7A3xfe0-9a-zA-Z\\s]";
      str =str.replaceAll(match, " ");
      return str;
   }

 

1
2
3
4
5
6
//이메일 유효성
   public static boolean isEmailPattern(String email){
    Pattern pattern=Pattern.compile("\\w+[@]\\w+\\.\\w+");
    Matcher match=pattern.matcher(email);
    return match.find();
   }

 

1
2
3
4
5
6
//연속 스페이스 제거
  public static String continueSpaceRemove(String str){
   String match2 = "\\s{2,}";
   str = str.replaceAll(match2, " ");
   return str;
  }


+ Recent posts