Wayneio Wayneio - 1 year ago 153
Java Question

Java split on second space

I'm trying to no avail, to split a sentence on every second space.

So given, this sentance:


you're a sausage bap


I have a working example of a single space split:

message.split("\\s+")


which gives me:


your

a

sausage

bap


but I need


your a

sausage bap


I've tried:

message.split("\\S+(\\s\\S+)?")


as per: Java. How can I split a string with multiple spaces on every nth space?

which doesn't work for me.

Answer Source

This should work

message.split("(?<!\\G\\w+)\\s")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download