这是我的第一个问题,抱歉我的英语不好
我想只从String中提取包含字母和数字组合的单词并将其存储在数组
中我尝试了这段代码,但我得不到我想要的东西
String temp = "74 4F 4C 4F 49 65 brown fox jump over the fence";
String [] word = temp.split("\\W");
这是我想要的结果(只有单词,没有空数组)
brown
fox
jump
over
the
fence
请帮助,谢谢!
答案 0 :(得分:2)
您可以使用:
String temp = "74 4F 4C 4F 49 65 brown fox jump over the fence";
List<String> arr = new ArrayList<String>();
Pattern p = Pattern.compile("(?i)(?:^|\\s+)([a-z]+)");
Matcher m = p.matcher(temp);
while (m.find())
arr.add(m.group(1));
// convert to String[]
String[] word = arr.toArray(new String[0]);
System.out.println( Arrays.toString(word) );
<强>输出:强>
[brown, fox, jump, over, the, fence]
答案 1 :(得分:2)
根据@ anubhava的回答,你可以做类似
的事情String temp = "74 4F 4C 4F 49 65 brown fox jump over the fence";
Pattern pattern = Pattern.compile("\\b[A-Za-z]+\\b");
Matcher matcher = pattern.matcher(temp);
while (matcher.find()) {
System.out.println("Matched " + matcher.group());
}