본문 바로가기

Programming/Java

[Java] 유용한 정규식(Regular expression) 정리

- 언어는 Java 를 기준으로 작성 되었습니다. 

- JavaScript에서는 lookbehind 기능을 지원하지 않기 때문에 동작하지 않을 수 있습니다.


1. 앞에서 n자리 뒤의 문자들을 마스킹 처리

String example = "mask1234";
example = example.replaceAll("(?<=.{4}).", "*");
System.out.println(example); // output : mask****


2. 1에서 20자리의 숫자로 된 문자만 허용

^[0-9]{1,20}$