Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String split by certain condition

Tags:

string

split

ruby

I'd like to split this string by ' ' only if it has ':':

"A:Hey B:Are C:You there"

C:You there should be not split. The result should be:

["A:Hey", "B:Are", "C:You there"]

How can I do this?

like image 981
ZHH Avatar asked Aug 23 '26 14:08

ZHH


1 Answers

\s+(?=\S*:)

You can split by this.

See demo.

https://regex101.com/r/hF7zZ1/4

This basically use a lookahead to make sure that the space which is being split upon is followed by non space characters and : .So it will work as you want.

like image 112
vks Avatar answered Aug 26 '26 04:08

vks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!