java - 仅当元音不以单词开头时才删除
问题描述
我正在尝试从长度超过 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”
解决方案
我会(?<=\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)
断言每个匹配元音之前的不是空格。
推荐阅读
- css - 是否可以检查是否定义了 CSS 变量?
- dart - 我可以通过条件导入隐藏某些环境的方法吗?
- javascript - amcharts 地图:点击时动态更改工具提示
- python - 在 flask-sqlAlchemy 中加入两个表并将其显示给 jinja2
- html - 在输入字段的角度将日期格式从 dd/mm/yyyy 更改为 yyyy/mm/dd
- python - BeautifulSoup 已安装但仍收到 ImportError: No module named BeautifulSoup
- reactjs - 如何向图像添加附加信息?
- javascript - Material UI - 我的卡片组件被添加到我的 Appbar 中。我希望在单击 Appbar 按钮后显示卡片
- python - RandomForestRegressor 和 graph_feature_importances 错误
- sip - SIP 200 OK to INVITE 然后 100 Trying