偶然發現網上貼出的使用正則表達式獲取字符串中的所有英文單詞或數字的方法,很實用,就摘下來了
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Test{
public static void main(String[] args){
String str="SUN公司被Oracle收購,是否意味着java被逼上了死路?";
String s = "\\d+.\\d+|\\w+";
Pattern pattern=Pattern.compile(s);
Matcher ma=pattern.matcher(str);
while(ma.find()){
System.out.println(ma.group());
}
}
}
正則表達式中“\d”表示[0-9]的數字,“\d+”表示由[0-9]的數字組成的數字,“\w”表示[A-Z0-9],“\w+”表示由數字、26 個英文字母或者下劃線組成的字符串,“\d+.+\d+”表示小數