[英]How to eliminate specific character with regex java
需要計算給定文本中的音節數。 一個或多個元音的每個連續序列,如果單詞有另一個元音或一組連續元音,則單詞末尾的單獨“e”除外,構成一個音節(將“y”視為元音)
public static void main(String[] args) {
// TODO Auto-generated method stub
int count =0;
String text = "This is a test. How many??? Senteeeeeeeeeences are here... there should be 5! Right?";
Pattern pat = Pattern.compile("[Ee]+(?!\\b)|[aiouyAIOUY]+");
Matcher m = pat.matcher(text);
while (m.find()) {
count++;
System.out.println(m.group());
}
System.out.println(count);
}
上面程序的輸出是15它必須是16當它是不包含任何元音的單詞中的最后一個字符時,應該消除 e 的計數,即,它不應消除 word(be) 中的 e 計數如何指定該條件在模式
嘗試這個
"(\\b[^aiouyeEAIOUY]+[Ee]\\b)|([aiouyAIOUY]\\b)|([aiouyeAIOUYE]{2,}\\b)|([aiouyeAIOUYE]+(?!\\b))"
以 driu 為目的的廣告:
Pattern pat = Pattern.compile("(\\b[^aiouye]+e\\b)|([aiouy]\\b)|([aiouye]{2,}\\b)|([aiouye]+(?!\\b))", Pattern.CASE_INSENSITIVE);
我觀察了 4 個要計算的場景(我將 4 個部分分組以更好地調試):
正確的解決方案
public static void main(String[] args) {
// TODO Auto-generated method stub
int count =0;
//String text = "Here is a series of test sentences. Your program should find 3 sentences, 33 words, and 49 syllables. Not every word will have the correct amount of syllables (example, for example), but most of them will.";
String text = "series";
Pattern pat = Pattern.compile("e(?!$)[aeiouy]*|[aieyou]*e(?!$)|[ayiou]+|\\b[^aiouye]+[e]\\b",Pattern.CASE_INSENSITIVE);
Matcher m = pat.matcher(text);
while (m.find()) {
count++;
System.out.println(m.group());
}
System.out.println(count);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.