Java Regex: repetitive groups?

后端 未结 3 1698
名媛妹妹
名媛妹妹 2021-01-22 05:57

How can I define repetitive groups in Java Regex?

Lets say a 2digit number [0-9]{2} multiple times separates by ,

12,34,98,11

Is that p

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-22 06:15

    That's supported in Java Regex. See Pattern documentation. Here's an example:

    "11,12,13".matches("\\d{2}(,\\d{2})*"); // true
    

    You can then split the string with String.split(), a Scanner or a StringTokenizer. Examples:

    String[] split = "11,12,13".split(",");
    StringTokenizer stringTokenizer = new StringTokenizer("11,12,13", ",");
    Scanner scanner = new Scanner("11,12,13").useDelimiter(",");
    while (scanner.hasNext()) {
        // scanner.next()
    }
    

提交回复
热议问题