728x90
https://www.regexplanet.com/advanced/java/index.html
정규식을 테스트하기에 좋은 사이트이다.
정규식을 자바에 옮길 때 '\'을 추가해야 하는 경우가 있는데, 자동으로 자바 문법에 맞게 변경해주고, matches 메소드와 find, group 메소드의 여부도 알려준다.
Pattern PATTERN = Pattern.compile("\\d+|\\D");
Pattern NUMSPACENUM = Pattern.compile("\\d+\\s+\\d");
Matcher numspacenum = NUMSPACENUM.matcher(input);
if (numspacenum.find()) {
throw new Exception("ERROR"); //숫자 공백 숫자
}
Pattern은 String 형태의 계산식을 숫자와 숫자가 아닌 것으로 parsing하는 정규식
NUMSPACENUM은 숫자 공백 숫자의 형식으로 입력이 들어왔을 때, 올바른 계산식이 아니므로 Exception을 발생시키기 위한 계산식이다.
728x90
'Java' 카테고리의 다른 글
[자바/Java] 제네릭 Generics (0) | 2022.07.27 |
---|---|
[자바/Java] 클래스 다형성(Polymorphism) (0) | 2022.07.25 |
[자바/Java] 클래스(Class), 상속, 생성자(Constructor), 오버라이딩(Override) (0) | 2022.07.25 |
[자바/Java] 싱글톤(Singleton) 패턴, Static (0) | 2022.07.25 |
[자바/Java] 배열, 1차원 배열, 2차원 배열 정리 (0) | 2022.07.23 |