String, broken by a specific condition

I would like to split this line into ' '

only if it has ':'

:

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

      

C:You there

should not be split. The result should be:

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

      

How can i do this?

+3


source to share


1 answer


\s+(?=\S*:)

      

You can split into this.

See demo.



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

Mostly use lookahead

to make sure the space

one on which the split follows non space characters and :

. So it will work the way you want it to.

+10


source







All Articles