// 正则获取某个字符前面的连续数字 \\d:连续数字
String content = "庞台672线检修;";
String content2 = "台庞672开关检修;";
String pattern = "\\d+线";
String pattern2 = "\\d+开关";
//String regex = "\\d++{5}"; --连续五位数字
// 创建 Pattern 对象
Pattern r = Pattern.compile(pattern);
Pattern r2 = Pattern.compile(pattern2);
// 现在创建 matcher 对象
Matcher m = r.matcher(content);
Matcher m2 = r2.matcher(content2);
if (m.find( )) {
System.out.println(m.group());
} else {
System.out.println("NO MATCH");
}
if (m2.find( )) {
System.out.println(m2.group());
} else {
System.out.println("NO MATCH2");
}
/**
* 正则取出票号
*/
public String regex(String str){
String regex = "FS+\\d+";
Pattern p = Pattern.compile(regex);
Matcher m1 = p.matcher(str.toUpperCase());
int i = 0;
while (m1.find()) {
if (m1.group() != null) {
str = m1.group();
}
}
return str;
}