How to eliminate specific character with regex java

时间:2015-11-12 11:51:36

标签: java regex

Need to count number of syllables in given text. Every contiguous sequence of one or more vowels, except for a lone “e” at the end of a word if the word has another vowel or set of contiguous vowels, makes up one syllable(Consider "y" as vowel)

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);
}

Output of above program is 15 It needs to be 16 It should to eliminate count of e's when it is last character in a word not containing any vowel i.e.., It should not eliminate count of e's in word(be) How to specify that condition in Pattern

2 个答案:

答案 0 :(得分:1)

试试这个

"(\\b[^aiouyeEAIOUY]+[Ee]\\b)|([aiouyAIOUY]\\b)|([aiouyeAIOUYE]{2,}\\b)|([aiouyeAIOUYE]+(?!\\b))"

以驱逐为目的:

Pattern pat = Pattern.compile("(\\b[^aiouye]+e\\b)|([aiouy]\\b)|([aiouye]{2,}\\b)|([aiouye]+(?!\\b))", Pattern.CASE_INSENSITIVE);

我观察了4个要计算的场景(我将4个部分分组以便更好地调试):

  1. e 结尾,
  2. 中没有其他元音
  3. 一个元音( e 除外)位于
  4. 字的末尾
  5. 两个或多个元音(包括 e )位于单词的末尾
  6. 一个或多个元音(包括 e )在单词中,但不在最后

答案 1 :(得分:0)

正确的解决方案

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);
}