ruby - How do I split on a "." but only if there are non-numbers following it?


Keywords:ruby 


Question: 

I want to split a line by a space, or a "." separating a number in front of it and a non-number behind it. I want to split like:

"10.ABC DEF GHI" # => ["10", "ABC", "DEF", "GHI"]
"10.00 DEF GHI" #=> ["10.00", "DEF", "GHI"]

I have

words = line.strip.split(/(?<=\d)\.|[[:space:]]+/)

But I discovered this doesn't quite do what I want. Although it will split the line:

line = "10.ABC DEF GHI"
words = line.strip.split(/(?<=\d)\.|[[:space:]]+/) # => ["10", "ABC", "DEF", "GHI"]

It will also incorrectly split

line = "10.00 DEF GHI"
line.strip.split(/(?<=\d)\.|[[:space:]]+/) # => ["10", "00", "DEF", "GHI"]

How do I correct my regular expression to only split on the dot if there are non-numbers following the "."?


1 Answer: 

Add a negative lookahead (?!\d) after \.:

/(?<=\d)\.(?!\d)|[[:space:]]+/
          ^^^^^^

It will fail the match if the . is followed with a digit.

See the Rubular demo.