【发布时间】:2020-09-30 04:59:56
【问题描述】:
我正在尝试从段落/字符串中提取单词。我搜索了很多地方,但没有找到相关材料。我想从
中提取长度为 4 的单词“我想在年纪大一点的时候有很多钱,可能是 e1X2”
我正在尝试使用
进行提取List<String> words = new ArrayList<String>();
String s = "I want to have alot of moneys when I am older probably.";
Pattern p = Pattern.compile("[a-zA-Z']{4,}");
Matcher m = p.matcher(s);
while (m.find()) {
words.add(m.group());
}
System.out.println(words);
我现在得到的输出
[want, have, alot, moneys, when, older, probably]
但输出必须是
[want, have, alot, when]
【问题讨论】:
-
单词可以包含数字吗?
-
只需将 {4,} 更改为 {4}
-
是的,它也可以包含数字
-
@RadheAnkit - 查看我更新的问题
-
我认为您可能需要添加look-ahead 和look-behind 以确保有一个空格字符(或字符串开头或结尾)分隔单词
标签: java android substring text-extraction stringtokenizer