한 범위를 다른 범위에서 빼서 새 범위로 사용할 수 있습니다. 두 가지 변형 문자 클래스(예:부정 및 교차)를 사용하여 이를 달성할 수 있습니다.
예를 들어 [a-l] 및 [^e-h] 범위의 교차는 문자 [e-h]를 뺀 분노로 문자 a에서 l을 제공합니다.
예시
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter input text: ");
String input = sc.nextLine();
String regex = "[a-l&&[^e-h]]";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(input);
int count =0;
while (matcher.find()) {
count++;
System.out.print(matcher.group()+" ");
}
System.out.println("Number of matched characters: "+count);
}
} 출력
Enter input text: abcdefghijklmnopq a b c d i j k l Number of matched characters: 8