1

it's a relatively easy java question with spliting.

public class Main {
    public static void main(String[] args) {
        String[] tokens = "aa33aaca^aa".split("[\\dc]+");
        for (int i = 0; i < tokens.length; i++)
            System.out.println(tokens[i]);  }
}

I'm wondering why is the output of this code is

aa
aa
a^aa

how is the "[\\dc]+" working exactly?

ZhaoGang
  • 3,447
  • 18
  • 28
  • I get how the third line of the output has been splited, due to the "c" but I'm confused with the other part :( –  Dec 18 '18 at 11:54
  • 2
    What is unclear? It does exactly what you ask it to do, if that is not what you expected, then please describe what you actually want to happen. And consider consulting the Java documentation on [`Pattern`](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html) – Mark Rotteveel Dec 18 '18 at 11:55
  • 1
    It's an regexp, Will split by a delimiter that is a number, or c character. So it's matching 33 and c. That is why you are receiving 3 results: aa,aa, a^aa. – Beri Dec 18 '18 at 11:55

2 Answers2

3

This regular expression will split the test by any combination of digits and character 'c' repeated at least once:

  • \\d - any digit [0-9]
  • c - character 'c'
  • []+ - characters inside of this set can occur at least once

So your string will be split like this:

   1     2
aa[33]aa[c]a^aa

Split delimiters:

  1. Digit releated 2 times.

  2. Character c.

Joakim Danielson
  • 29,280
  • 4
  • 14
  • 35
Beri
  • 10,277
  • 3
  • 24
  • 47
1

Besides the excellent answers above, Note that, if you test [\\dc]+ in an online regex tester,like rubular or others, this regex will not match 33 in aa33aaca^aa. And they will say that : \d is mapping to Any digit character(0-9).

The reason is that in String.split() we are take String as our parameters, so we need an additional \ to form a escape sequence to let the \d take effect.

ZhaoGang
  • 3,447
  • 18
  • 28