2008年11月23日星期日

如何才能仅匹配英文?

半天也没搞出来

[code]

public String processFile() {
String line, resultStr;
StringBuffer sb = new StringBuffer();
String regex=new String();
try {
FileInputStream fr = new FileInputStream(path);
InputStreamReader isr = new InputStreamReader(fr);
BufferedReader br = new BufferedReader(isr);

Matcher m;
Pattern p = null;
switch (index) {
case 0://匹配中文
regex = "[\u4e00-\u9fa5]";
break;
case 1://匹配英文
regex = "\\w";
break;
case 3://匹配代码
regex="^[a-zA-Z[#(/\\*)]].*[\\p{Punct}]$";
}
if (regexMore.equals("") || null == regexMore) {
regexMore = "";
p=Pattern.compile(regex);
}else{
p=Pattern.compile(regexMore+regex);
}
while ((line = br.readLine()) != null) {
m = p.matcher(line);
if (m.find()) {
sb.append(line + "\r\n");
}

}
} catch (Exception e) {
e.printStackTrace();
resultStr = "路径出错!";
return resultStr;
}
return resultStr = sb.toString();
}

[/code]

就是匹配不了全英文,郁闷了

没有评论: