含字母字符串对阿拉伯数字加减再取得结果字符串(字符串的正则表达式处理)
背景:
对字符串00000302Q3NL04445298 求得数字+n的结果。如n=1 得到结果字符串00000302Q3NL04445299
举例用法: String reg1 = "\\D+(\\d+)$"; //提取字符串末尾的数字 String reg2 = "\\D+(\\d+)\\D+"; // 提起字符串中间的数字 String reg3 = "\\D+(\\d+)\\D+(\\d+)\\D*"; // 提起字符串中间的多个数字 String s = monster.getMonsterName(); Pattern p = Pattern.compile(reg); Matcher m = p.matcher(s); int historyHighestLevel = 1; if(m.find()){ historyHighestLevel = Integer.parseInt(m2.group(1)); System.out.println(m.group(1)); // 组提取字符串 }
贴代码:
public class aaa {
public static void main(String[] args) {
//入参1
String startSN = "00000302Q3NL04445298";
//入参2
int numsOperation = 2;
String