Java의 정규식이란 무엇입니까?
정규식 또는 정규식 은 패턴을 형성하는 일련의 문자입니다. 데이터를 검색할 때 이 특수 패턴을 사용하여 일치하는 문자열을 찾을 수 있습니다. 단일 문자만큼 간단할 수도 있고 더 복잡한 패턴일 수도 있습니다. Java에는 패턴 검색 및 조작을 위해 정규식을 사용하는 기능을 제공하는 클래스가 포함된 Java regex 패키지가 있습니다. 이 패키지는 다음과 같은 방법으로 코드로 가져옵니다.import java.util.regex.*;
Java 정규식 - 일치자
import java.util.regex.Matcher;
Java 정규식 패키지에는 세 가지 클래스가 있습니다.
-
패턴 클래스: 검색해야 하는 패턴을 정의합니다.
-
Matcher 클래스: 텍스트의 패턴을 일치시켜 찾습니다.
-
PatternSyntaxException 클래스: 정규식의 구문 오류를 나타냅니다.
Java Matcher 클래스 메서드
Java Matcher 클래스 메소드 중 일부가 아래에 설명되어 있습니다.-
boolean match() — 정규식이 패턴과 일치하면 true를 반환합니다.
-
boolean find() — 패턴과 일치하는 다음 정규식을 찾습니다.
-
boolean find(int start) — 주어진 시작부터 패턴과 일치하는 다음 정규식을 찾습니다.
-
int start() — 일치 항목의 시작 인덱스를 반환합니다.
-
int end() — 반환된 일치 항목의 끝 인덱스를 반환합니다.
-
int groupCount() — 일치하는 그룹의 총 개수를 반환합니다.
-
String replacementAll(String replacement) — 일치하는 모든 하위 시퀀스를 지정된 대체 항목으로 바꿉니다.
-
String replacementFirst(String replacement) — 일치 항목의 첫 번째 하위 시퀀스를 지정된 대체 항목으로 바꿉니다.
Java 일치자 예
Matcher 클래스 에는 유용한 메서드가 많이 있지만 match() 메서드 의 구체적인 예를 더 자세히 살펴보겠습니다 .match() 메서드 사용
다음은 match() 메서드 에 대한 Java Matcher 예제를 보여줍니다 .import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
String regexPattern = "(.*)apple(.*)"; // this regex means that any char sequence can precede or succeed "apple"
String firstCheck = "Apples";
String secondCheck = "Apple";
String thirdCheck = "apple";
String fourthCheck = "An apple a day keeps the doctor away.";
String fifthCheck = "green apple, yellow apple, red apple, i love all kinds of apples.";
Pattern pattern = Pattern.compile(regexPattern);
Matcher matcher1 = pattern.matcher(firstCheck);
System.out.println("The Pattern is: " + pattern);
System.out.println("Text to check is: " + firstCheck);
System.out.println("Output for matches(): " + matcher1.matches() + "\n");
Matcher matcher2 = pattern.matcher(secondCheck);
System.out.println("The Pattern is: " + pattern);
System.out.println("Text to check is: " + secondCheck);
System.out.println("Output for matches(): " + matcher2.matches() + "\n");
Matcher matcher3 = pattern.matcher(thirdCheck);
System.out.println("The Pattern is: " + pattern);
System.out.println("Text to check is: " + thirdCheck);
System.out.println("Output for matches(): " + matcher3.matches() + "\n");
Matcher matcher4 = pattern.matcher(thirdCheck);
System.out.println("The Pattern is: " + pattern);
System.out.println("Text to check is: " + fourthCheck);
System.out.println("Output for matches(): " + matcher4.matches() + "\n");
Matcher matcher5 = pattern.matcher(fifthCheck);
System.out.println("The Pattern is: " + pattern);
System.out.println("Text to check is: " + fifthCheck);
System.out.println("Output for matches(): " + matcher5.matches() + "\n");
}
}
산출
패턴: (.*)apple(.*) 확인할 텍스트: Apples match()에 대한 출력: false 패턴: (.*)apple(.*) 확인할 텍스트: Apple match()에 대한 출력 : false 패턴: (.*)apple(.*) 확인할 텍스트: apple match()에 대한 출력: true 패턴: (.*)apple(.*) 확인할 텍스트: 하루에 사과 한 개 의사를 멀리합니다. match()에 대한 출력: true 패턴은: (.*)apple(.*) 확인할 텍스트는: green apple, yellow apple, red apple, 나는 모든 종류의 사과를 좋아합니다. match()에 대한 출력: true