2013-11-02 49 views
1

这是我的第一个问题,我的英语不好从字符串中提取字母和数字在java中的组合?

抱歉,我想从字符串,有字母和数字的组合,只提取字,并将其存储在阵列

我试试这个代码,但我不吨得到我想要的东西

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 

普莱斯e帮助,谢谢!

回答

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

OUTPUT:

[brown, fox, jump, over, the, fence] 
+0

这也给'FCF' =( –

+0

我尝试你的代码,但数组的第一个索引是空的,字母f ROM 4F 4C 4F也在阵列中。对不起,我的英语不好。 –

+0

正则表达式很容易被修复 String [] word = temp.split(“\ w [^ A-Za-z] + \ w”); (假设Java支持\ w字边界指示符)。 – CompuChip

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