0

I want to using regex on Java to split a number string. I using a online regex tester test the regex is right. But in Java is wrong.

Pattern pattern = Pattern.compile("[\\\\d]{1,4}");
String[] results = pattern.split("123456");
// I expect 2 results ["1234","56"]
// Actual results is ["123456"]

Anything do I missing?


I knows this question is boring. But I wanna to solve this problem. Answer

Pattern pattern = Pattern.compile("[\\d]{1,4}");
String[] results = pattern.split("123456");
// Results length is 0
System.out.println(results.length);

is not working. I have try it. It's will return nothing on the results. Please try before answer it.

Sincerely thank the people who helped me.


Solution:

Pattern pattern = Pattern.compile("([\\d]{1,4})");
Matcher matcher = pattern.matcher("123456");
List<String> results = new ArrayList<String>();
while (matcher.find()) {
    results.add(matcher.group(1));
}

Output 2 results ["1234","56"]

Gordian Yuan
  • 4,750
  • 6
  • 29
  • 35
  • You are getting an empty array, because split returns the parts *between* the delimiters to split on, throwing away the delimiters themselves. Maybe this helps: http://stackoverflow.com/questions/2206378/how-to-split-a-string-but-also-keep-the-delimiters http://stackoverflow.com/questions/275768/is-there-a-way-to-split-strings-with-string-split-and-include-the-delimiters – Thilo Mar 16 '12 at 08:12

3 Answers3

5
Pattern pattern = Pattern.compile("[\\\\d]{1,4}")

Too many backslashes, try [\\d]{1,4} (you only have to escape them once, so the backslash in front of the d becomes \\. The pattern you wrote is actually [\\d]{1,4} (a literal backslash or a literal d, one to four times).

When Java decided to add regular expressions to the standard library, they should have also added a regular expression literal syntax instead of shoe-horning it over Strings (with the unreadable extra escaping and no compile-time syntax checking).

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Pattern pattern = Pattern.compile("\\d{1,4}"); String[] results = pattern.split("123456"); The results length is zero. – Gordian Yuan Mar 16 '12 at 03:39
1

Solution:

Pattern pattern = Pattern.compile("([\\d]{1,4})");
Matcher matcher = pattern.matcher("123456");
List<String> results = new ArrayList<String>();
while (matcher.find()) {
    results.add(matcher.group(1));
}

Output 2 results ["1234","56"]

Gordian Yuan
  • 4,750
  • 6
  • 29
  • 35
1

You can't do it in one method call, because you can't specify a capturing group for the split, which would be needed to break up into four char chunks.

It's not "elegant", but you must first insert a character to split on, then split:

String[] results = "123456".replaceAll("....", "$0,").split(",");

Here's the output:

System.out.println(Arrays.toString(results)); // prints [1234, 56]

Note that you don't need to use Pattern etc because String has a split-by-regex method, leading to a one-line solution.

Bohemian
  • 412,405
  • 93
  • 575
  • 722