-2

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.

Community
  • 1
  • 1
Wayneio
  • 3,466
  • 7
  • 42
  • 73
  • 1
    I would implement it myself, without using regexes or the built-in `split()` method. :) – Konstantin Yovkov Oct 02 '17 at 12:05
  • Output and input looks same.. Provide the expected input and output – Samuel Robert Oct 02 '17 at 12:07
  • 1
    The question you cite shows two different approaches. One of them uses `String.split()` the other one `Pattern.compile()` to consume a regex. You mixed them up. You can't use this regex with `split()`! – blafasel Oct 02 '17 at 12:10

1 Answers1

0

This should work

message.split("(?<!\\G\\w+)\\s")
Samuel Robert
  • 10,106
  • 7
  • 39
  • 60
  • Thanks. This helped me realise that the other answers I saw in other questions, once copied across added extra backslashes – Wayneio Oct 02 '17 at 12:22