대괄호 "[ ] 내에서 일치하도록 필요한 모든 문자를 그룹화할 수 있습니다. " 즉, 메타 문자/하위 표현 "[ ] "는 지정된 모든 문자와 일치합니다. 따라서 모든 문자를 일치시키려면 다음과 같이 이 안에 모음 문자를 지정하십시오. -
[aeiouAEIOU]
예시 1
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatchVowels {
public static void main( String args[] ) {
String regex = "[aeiouAEIOU]";
System.out.println("Enter input string: ");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
//Compiling the regular expression
Pattern.compile(regex);
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if(matcher.find()) {
System.out.println("The input string contains vowels");
} else {
System.out.println("The input string does not contain vowels");
}
}
} 출력
Enter input string: hello how are you welcome The input string contains vowels
예시 2
import java.util.Scanner;
public class Test {
public static void main( String args[] ) {
String regex = "[aeiouAEIOU]";
System.out.println("Enter input string: ");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
boolean result = input.matches(regex);
if(result) {
System.out.println("The input string contains vowels");
} else {
System.out.println("The input string does not contain vowels");
}
}
} 출력
Enter input string: hello how are you welcome The input string does not contain vowels