我想从输入字符串中删除包含比辅音更多的元音的单词。 我希望为此使用正则表达式,有人可以给我任何建议吗?
Input: eef geggughhht oaiu hjekloykj
Output: geggughhht hjekloykj
这是我的一些伪代码
String str = "eef geggughhht oaiu hjekloykj";
Pattern pattern = Pattern.compile("[aeiou]+");
for (String ch : str.split(" ")) {
Matcher matcher = pattern.matcher(ch);
int countVowels = 0;
int countConsonants = 0;
for(int i = 0; i < ch.length(); i++) {
if(matcher.find(i)){
countVowels++;
} else {
countConsonants++;
}
}
if (countVowels > countConsonants){
System.out.println();
} else {
System.out.println(ch);
}
}
答案 0 :(得分:0)
您可以使用这种方法:
String str = "eef geggughhht oaiu hjekloykj";
final Pattern p = Pattern.compile("[aeiou]+", Pattern.CASE_INSENSITIVE);
String[] arr = str.split("\\s+");
for (int i=0; i<arr.length; i++) {
String cstr = p.matcher(arr[i]).replaceAll("");
if ( cstr.length() >= (arr[i].length() - cstr.length()) ) {
System.out.println(arr[i]);
}
}
geggughhht
hjekloykj
使用这种方法,我们删除了字符串中每个单词的所有元音,然后将剩余字符串的长度(包括所有辅音)与元音的字符串进行比较。