首页 > 解决方案 > 仅当元音不以单词开头时才删除

问题描述

我正在尝试从长度超过 10 个字符的字符串中删除元音,除非它们以字符串开头或以单词开头。我不知道如何只删除那些特定的元音。

我尝试过使用 string.replaceAll,以及各种间距组合并尝试分隔元音。我对java非常陌生,不知道还能做什么。我发现完美描述我的问题的唯一其他线程是python。

String x = scan.nextLine();
    if(x.length() >= 10){
        x.toLowerCase();
        String y = x.replaceAll("[aeiou]", "");
        System.out.println(y);
    }else{    
        System.out.println("This doesn't need shortening!");

如果用户输入“The quick brown fox jumps over the极大懒狗”,输出应该是“Th qck brwn fx jmps ovr th extrmly lzy dg”

标签: java

解决方案


我会(?<=\S)[aeiou]在不区分大小写的模式下使用该模式:

String input = "Hello how are you?";
System.out.println(input);
input = input.replaceAll("(?i)(?<=\\S)[aeiou]", "");
System.out.println(input);

这打印:

Hello how are you?
Hll hw ar y?

肯定的后视(?<=\S)断言每个匹配元音之前的不是空格。


推荐阅读