如何匹配字符串中的数字整数并返回整数值。?例如;
String = "Color Red, Size 32 / Text text";
我想从这个字符串中得到“32”整数值。
提前谢谢了。
问候,科科
public static void main(String[] args) {
String s = "Color Red, Size 32 / Text text";
Matcher matcher = Pattern.compile("\\d+").matcher(s);
if (matcher.find()) {
String find = matcher.group();
Integer i = Integer.parseInt(find);
}
}
试试这个代码
Pattern p = Pattern.compile("^[a-zA-Z]+([0-9]+).*");
Matcher m = p.matcher("Testing1234Testing");
if (m.find()) {
System.out.println(m.group(1));
}