Mars-Wang 2019-04-11 14:58 采纳率: 0%
浏览 368

3-7,9-10怎么用正则表达式拆分成3 4 5 6 7 9 10

3-7,9-10怎么用正则表达式拆分成3 4 5 6 7 9 10
1091011怎么拆分成1 9 10 11

  • 写回答

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); //结果
                }
            }
    

    第二个规律不明显

    评论

报告相同问题?