지속적 업데이트 예정
Pattern.compile() | 정규표현식을 패턴으로 컴파일 | Pattern pattern = Pattern.compile("\\d+"); |
matcher() | 입력 문자열을 검사할 Matcher 객체 반환 | Matcher matcher = pattern.matcher("12345"); |
matches() | 입력 문자열 전체가 패턴과 일치하는지 확인 | matcher.matches(); // true |
find() | 입력 문자열에서 패턴과 일치하는 부분을 찾음 (여러 번 호출 가능) | matcher.find(); // true (일치하는 부분이 있으면 true 반환) |
group() | 매칭된 전체 부분 또는 첫 번째 그룹을 반환 | matcher.group(); // "12345" |
group(int group) | 특정 그룹 번호에 해당하는 부분 반환 | matcher.group(1); // 그룹 매칭 반환 |
replaceAll() | 패턴과 일치하는 모든 부분을 치환 | "abc123".replaceAll("\\d", "#"); // "abc###" |
replaceFirst() | 패턴과 일치하는 첫 번째 부분만 치환 | "abc123".replaceFirst("\\d", "#"); // "abc#23" |
split() | 패턴을 기준으로 문자열을 분리 | String[] result = pattern.split("a,b,c"); |
lookingAt() | 문자열의 시작 부분이 패턴과 일치하는지 확인 | matcher.lookingAt(); // true |
start() | 매칭된 부분의 시작 인덱스를 반환 | matcher.start(); // 10 (매칭된 첫 번째 인덱스) |
end() | 매칭된 부분의 끝 인덱스를 반환 | matcher.end(); // 15 (매칭된 마지막 인덱스) |
quote() | 메타 문자가 있는 문자열을 특수 문자 그대로 매칭 | Pattern.quote("example.com"); // example\\.com (정확한 텍스트 매칭) |
Pattern.flags() | 패턴을 생성할 때 사용된 플래그 반환 | Pattern pattern = Pattern.compile("pattern", Pattern.CASE_INSENSITIVE); int flags = pattern.flags(); |
Pattern.splitAsStream() | 패턴을 기준으로 문자열을 분리하고 스트림 반환 | Pattern.compile(",").splitAsStream("a,b,c").forEach(System.out::println); |
reset() | Matcher의 상태를 초기화하여 동일한 패턴으로 다른 문자열 검색 | matcher.reset("new text"); |
region(int start, int end) | 검색할 문자열의 영역을 지정 | matcher.region(5, 10); (부분 문자열만 검색) |
usePattern() | 새로운 패턴을 사용하도록 설정 | matcher.usePattern(newPattern); |
toString() | Pattern 또는 Matcher 객체의 문자열 표현 반환 | System.out.println(pattern.toString()); |
사용 예시
1. Pattern.compile()과 matcher(), find()
java
코드 복사
Pattern pattern = Pattern.compile("\\d+"); // 숫자 패턴
Matcher matcher = pattern.matcher("There are 123 apples");
if (matcher.find()) {
System.out.println(matcher.group()); // "123" 출력
}
2. replaceAll() 메서드를 사용한 문자열 치환
String result = "Hello123World".replaceAll("\\d", "#");
System.out.println(result); // "Hello###World" 출력
3. split() 메서드를 사용한 문자열 분리
Pattern pattern = Pattern.compile(",");
String[] fruits = pattern.split("apple,orange,banana");
for (String fruit : fruits) {
System.out.println(fruit); // "apple", "orange", "banana" 각각 출력
}
4. find()와 group()을 사용한 여러 패턴 찾기
find()는 입력 문자열에서 여러 번 호출될 수 있으며, 매칭된 여러 패턴을 찾을 수 있습니다.
java
코드 복사
Pattern pattern = Pattern.compile("\\d+"); // 숫자 패턴
Matcher matcher = pattern.matcher("123 and 456 and 789");
while (matcher.find()) {
System.out.println("Found: " + matcher.group()); // "123", "456", "789" 출력
}
5. start()와 end()를 사용하여 매칭된 부분의 시작과 끝 인덱스 찾기
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("Order number is 12345");
if (matcher.find()) {
System.out.println("Start index: " + matcher.start()); // 16
System.out.println("End index: " + matcher.end()); // 21
}
6. lookingAt()을 사용하여 문자열 시작 부분에서 패턴 찾기
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("123abc");
if (matcher.lookingAt()) {
System.out.println("Found at the start: " + matcher.group()); // "123"
}
7. replaceFirst()와 replaceAll()을 사용하여 문자열 치환
특정 패턴을 첫 번째만 또는 모든 패턴을 치환하는 예시입니다.
// 첫 번째 패턴만 치환
String text = "hello123world456";
String replaced = text.replaceFirst("\\d+", "#");
System.out.println(replaced); // "hello#world456"
// 모든 패턴 치환
String replacedAll = text.replaceAll("\\d+", "#");
System.out.println(replacedAll); // "hello#world#"
5. split()을 사용하여 문자열을 패턴으로 분리
쉼표 또는 다른 구분자를 기준으로 문자열을 분리하는 예시입니다.
Pattern pattern = Pattern.compile(",");
String[] result = pattern.split("apple,orange,banana");
for (String word : result) {
System.out.println(word); // "apple", "orange", "banana"
}
6. region()을 사용하여 특정 영역만 검색
문자열의 특정 부분만 검색하도록 region() 메서드를 사용할 수 있습니다.
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("123abc456def");
matcher.region(6, 9); // 6번째 인덱스부터 9번째 인덱스까지의 영역만 검색
if (matcher.find()) {
System.out.println("Found in region: " + matcher.group()); // "456"
}
7. quote()를 사용하여 메타 문자를 포함한 텍스트 매칭
정확한 텍스트 매칭을 위해 Pattern.quote()를 사용하여 메타 문자를 그대로 매칭할 수 있습니다.
Pattern pattern = Pattern.compile(Pattern.quote("example.com"));
Matcher matcher = pattern.matcher("Visit example.com for details");
if (matcher.find()) {
System.out.println("Exact match found: " + matcher.group()); // "example.com"
}
'JAVA > Regex' 카테고리의 다른 글
자바 이스케이프 처리 (4) | 2024.10.17 |
---|---|
정규표현식 표로 정리해보자 + 자주 쓰는 정규표현식 모음 (1) | 2024.10.17 |