3-7,9-10怎么用正则表达式拆分成3 4 5 6 7 9 10
1091011怎么拆分成1 9 10 11
3-7,9-10怎么用正则表达式拆分成3 4 5 6 7 9 10
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
迷路的公民 2019-04-11 15:11关注第一个可以通过
String str = "3-7,9-10";//输入 Pattern p = Pattern.compile("([1-9][0-9]{0,}-[1-9][0-9]{0,})"); Matcher matcher = p.matcher(str); while(matcher.find()) { String group = matcher.group(); String[] strs = group.split("-"); int firstNum = Integer.parseInt(strs[0]); int secondNum = Integer.parseInt(strs[1]); for(;firstNum<=secondNum;firstNum++) { System.out.println(firstNum); //结果 } }第二个规律不明显
解决 无用评论 打赏 举报