0

I found that split regex in java is working as greedy

String str = ";;;";
System.out.println(str.split(";").length);

output - 0 (wrong)

expected - 4

String str = ";;;a";
System.out.println(str.split(";").length);

output - 4

I tried to modified the regex and make it lazy using regex as ;+? but got output as 0.

Any idea how to make regex as greedy for split here will be much appreciated.

Thanks in advance

2
  • what do you mean with greedy? Commented May 9, 2013 at 7:09
  • The regex pattern looks like consuming all ";;;" and so the length after split is 0. Commented May 9, 2013 at 7:16

4 Answers 4

2

You need to specify the limit, to achieve what you want.

str.split(";", -1); // -1 is the limit, which will make the split method greedy as you want.

Non-Positive limit means that the pattern will be applied as many times as possible!

Therefore System.out.println(str.split(";").length); will now print 4, as required.

Have a look at the docs for more detailed info.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot. This works like a charm. May be i should have taken a look on the java docs.
@user2365199 please mark it as answer (left side tick mark) if you found helpful.
@user2365199 - Yes, you must look into the docs before using any method to know its complete functionality. Nevertheless, glad to have helped you!:)
0

Try

 String str = ";;;"; 
 System.out.println(str.split(";",-1).length); //LIMIT missed 

Comments

0

String.split(String s) API says that trailing empty strings are not included in the resulting array. If you want them to be included try with unlimited limit

    String str = ";;;";
    System.out.println(str.split(";", Integer.MAX_VALUE).length);

Comments

0

It has nothing to do with greediness. It has to do with the split() implementation.

By default, all trailing blank matches are ignored. Since you only have blank matches, all matches are discarded.

To override this behaviour of ignoring trailing blanks, invoke split with a second parameter of -1;

str.split(";", -1);

This second parameter n is the limit and the javadoc says:

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

See the javadoc for split() more detail.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.