Armando Pérez
Armando Pérez

Reputation: 182

Regex to match space followed by one or more character

I need to break down a string into three lines. Each line should be formed by a word, not a character.

For exmaple:

G G YELLOW/WHITE CORN

must return

G G
YELLOW/WHITE
CORN.

So far I have:

String[] spitted= sentence.split("[a-zA-Z]{2,}",3);

it returns:

 G G 
 /
 CORN

I think my solution is to use a regular expession to match a space followed by a word, not a character, but I'm not good at regexp.

The laguage I'm using is java.

Upvotes: 2

Views: 5010

Answers (1)

anubhava
anubhava

Reputation: 785028

You can use this regex for splitting:

\s+(?=\S{2})

Which means split on 1 or more space followed by at least 2 or more non-space characters.

RegEx Demo

For Java use:

String[] spitted = sentence.split("\\s+(?=\\S{2})");

Upvotes: 4

Related Questions