简体   繁体   中英

RegEx - Split string by word

I've been trying to write a sort of surreal text adventure game for my daughter, everything has been going well but I've got stuck with trying to process a particular input with RegEx.

The input string needs to begin with a number (optionally decimal), followed by either a series of characters (min 1, max n ) immediately followed by a whitespace, then and , or followed instead by a white-space and then n amount of words (no numbers) until the split word (and). Any words after "and" need to be captured as well in another group.

So an example valid input might be:

5 blue cows and a bucket of milk

or

2.5mph and a really slow car

and split into three groups like so:

[5] [blue cows] [a bucket of milk] or [2.5] [mph] [a really slow car]

Being new to RegEx I'm going to apologise in advance for my terrible attempts, but I believe I need to use a lookahead ?

My attempts so far look a bit like this:

Works fine but only captures a single word each side, eg: [5] [blue] [a]

^(\d*\.?\d*)\s*([a-z]*)\s*\band\b\s([a-z]+)

And this one using the lookahead captures multiple words as expected but each individual character is captured separately:

^(\d*\.?\d*)\s*(.?)+(?=and)\s*\band\b\s((.?)+)

Any help would be really appreciated, especially as my daughter's answer to the problem was something along the lines of "Daddy you should shave off your moustache, then it would work?"

Cheers guys :)

Steve

I think your problem is that you didn't include the space in the matching group. Check this regex:

^(\d*\.?\d*)\s*([a-z\s]*)\sand\s([a-z\s]*)$

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