Timmy Von Heiss
Timmy Von Heiss

Reputation: 2218

how can i split a string if there is a period and then a space?

Right now I am using .split(/(?<=[.])/) which splits keeps the period.

string = "Going home. See you soon." 

=> ["Going home.", "See you soon."]

But what I need is to split on and keep ". "

Upvotes: 1

Views: 232

Answers (2)

Md. Farhan Memon
Md. Farhan Memon

Reputation: 6121

If it is that straight forward, why you need a regex, just provide the string to split on as

"Going home. See you soon.".split('. ').map{ |s| "#{s}." }

Upvotes: 1

falsetru
falsetru

Reputation: 369034

Using look-behind assertion, you can match space () following . ((?<=\.)):

string = "Going home. See you soon."
string.split(/(?<=\.) /)
=> ["Going home.", "See you soon."]

Upvotes: 3

Related Questions