今天想要截取一串字符中的字母0+130 ,想了一会儿还是正则比较好用点,直接粘测试的代码:
public static void main(String[] args){
String regEx="([0-9][+-][0-9]+[.][0-9]+)|([0-9][+-][0-9]+)";
Pattern compile = Pattern.compile(regEx);
String name = "0+130绕流渗压计RP-1-04";
Matcher matcher = compile.matcher(name);
if (matcher.find(0)){
String group = matcher.group();
System.out.println(group);
}
}
matcher.find(0)为找到索引为0的匹配的字符串,然后通过matcher.group()进行获取
有的时候我们并不需要获取到其中的字符串,而是只需要去匹配字符串是否符合该正则表达式
boolean matches = matcher.matches();
返回的就是Boolean类型的值
在开发过程中,正则表达式是一种使用很频繁的工具,正确使用,能很大程度的提高开发效率。