Java Regex: repetitive groups?

后端 未结 3 1695
名媛妹妹
名媛妹妹 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()
    }
    
    0 讨论(0)
  • 2021-01-22 06:16

    The simplest way is to use a two-step solution: 1) first, validate the string, then 2) split the string with the delimiter of your choice:

    String[] chunks = null;
    if (s.matches("\\d{2}(?:,\\d{2})*")) {
        chunks = s.split(Pattern.quote(","));
        System.out.println(Arrays.toString(chunks)); // => [12, 34, 98, 11]
    }
    

    Here, s.matches("\\d{2}(?:,\\d{2})*") matches the whole string that starts with two digits and then contains 0 or more occurrences of , and two digits to the end, and then s.split(Pattern.quote(",")) splits the string with a comma. Note you do not need ^ and $ anchors with a pattern inside matches() since the method requires a full string match.

    If you have to do that with a single regex, you may use multiple matches anchored to the start of string and end of each successful preceding match, only if the string check at the start of the string is a success:

    (?:\G(?!^),|^(?=\d{2}(?:,\d{2})*$))(\d{2})
    

    See the regex demo.

    Details

    • (?:\G(?!^),|^(?=\d{2}(?:,\d{2})*$)) - end of the preceding successful match and then a , (see \G(?!^),) or (|) start of string (^) that is followed with two digits and then 0 or more sequences of a , and two digits to the end of the string (see \d{2}(?:,\d{2})*$)
    • (\d{2}) - Group 1: two digits

    Java demo:

    String s = "12,34,98,11";
    Pattern p = Pattern.compile("(?:\\G(?!^),|^(?=\\d{2}(?:,\\d{2})*$))(\\d{2})");
    Matcher m = p.matcher(s);
    List<String> results = new ArrayList<>();
    while(m.find()) {
        results.add(m.group(1));
    }
    System.out.println(results); 
    // => [12, 34, 98, 11]
    
    0 讨论(0)
  • 2021-01-22 06:39

    In Java you may also use Scanner APIs for this:

    final Pattern pat = Pattern.compile("\\d{2}");
    Scanner scan = new Scanner("12,34,98,11");
    scan.useDelimiter(",");
    while(scan.hasNext(pat)) {
        System.out.println( "Token: " + scan.next() );
    }
    scan.close();
    
    0 讨论(0)
提交回复
热议问题