简体   繁体   中英

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.

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})");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM