Skip to content Skip to sidebar Skip to footer

Regex For String Splitting Not Working Properly

I want to split a string in 2 characters without any delimiter, but Regex split is not working properly here is my code:- String str='splitstring'; System.out.println('Split.

Solution 1:

Looks like it's a bug with the result threshold limit in your Java environment. Try to workaround it by providing the limit explicitly:

    String str="splitstring";
    int partsCount = (str.length() + 1) / 2;
    System.out.println("Split.."+str.trim().split("(?<=\\G.{2})", partsCount).length);
    System.out.println("Split.."+str.trim().split("(?<=\\G.{2})", partsCount)[0]);
    System.out.println("Split.."+str.trim().split("(?<=\\G.{2})", partsCount)[1]);

Solution 2:

I assume you want to divide string into two equal halves, substrings.. Then this might help !!

System.out.println("Split.."+str.trim().split("(?=\\D)")[0]);

Solution 3:

Why do you use a regex for this?

You can do something like this:

String str = "splitstring";
System.out.println("String-length: " + str.length());
for (int i = 0; i < str.length(); i += 2) { // Increments of 2
  System.out.print("Split.." + str.charAt(i));
  if (i != str.length() - 1) {
    System.out.print(str.charAt(i + 1));
  }
  System.out.println();
}

Output:

String-length: 11Split..spSplit..liSplit..tsSplit..trSplit..inSplit..g

EDIT: If you insist of using the regex though, here it is:

finalStringstr = "splitstring";
System.out.println(Arrays.toString(
    str.split("(?<=\\G.{2})")
));

Output remains the same as above. Seems like there is indeed a bug or something, since the regex is the same as in your question.. Can't really say for sure though, since it's not really my expertise.


EDIT 2: Jon Skeet has an alternative method that is more efficient than mine above:

Well, it's fairly easy to do this by brute force:

publicstaticList<String> splitEqually(String text, int size) {
    // Give the list the right capacity to start with. You could use an array// instead if you wanted.List<String> ret = newArrayList<String>((text.length() + size - 1) / size);

    for (int start = 0; start < text.length(); start += size) {
        ret.add(text.substring(start, Math.min(text.length(), start + size)));
    }
    return ret;
}

I don't think it's really worth using a regex for this.

EDIT: My reasoning for not using a regex:

  • This doesn't use any of the real pattern matching of regexes. It's just counting.
  • I suspect the above will be more efficient, although in most cases it won't matter
  • If you need to use variable sizes in different places, you've either got repetition or a helper function to build the regex itself based on a parameter - ick.
  • The regex provided in another answer firstly didn't compile (invalid escaping), and then didn't work. My code worked first time. That's more a testament to the usability of regexes vs plain code, IMO.

Post a Comment for "Regex For String Splitting Not Working Properly"