Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep the delimiters when splitting a Ruby string?

Tags:

ruby

I have text like:

content = "Do you like to code? How I love to code! I'm always coding." 

I'm trying to split it on either a ? or . or !:

content.split(/[?.!]/)

When I print out the results, the punctuation delimiters are missing.

Do you like to code

How I love to code

I'm always coding

How can I keep the punctuation?

like image 306
BrainLikeADullPencil Avatar asked Aug 06 '13 20:08

BrainLikeADullPencil


People also ask

How do you split a character in a string in Ruby?

split is a String class method in Ruby which is used to split the given string into an array of substrings based on a pattern specified. Here the pattern can be a Regular Expression or a string. If pattern is a Regular Expression or a string, str is divided where the pattern matches.

When splitting a string using a given separator it returns?

The Split method extracts the substrings in this string that are delimited by one or more of the strings in the separator parameter, and returns those substrings as elements of an array.


2 Answers

Answer

Use a positive lookbehind regular expression (i.e. ?<=) inside a parenthesis capture group to keep the delimiter at the end of each string:

content.split(/(?<=[?.!])/)

# Returns an array with:
# ["Do you like to code?", " How I love to code!", " I'm always coding."]

That leaves a white space at the start of the second and third strings. Add a match for zero or more white spaces (\s*) after the capture group to exclude it:

content.split(/(?<=[?.!])\s*/)

# Returns an array with:
# ["Do you like to code?", "How I love to code!", "I'm always coding."]

Additional Notes

While it doesn't make sense with your example, the delimiter can be shifted to the front of the strings starting with the second one. This is done with a positive lookahead regular expression (i.e. ?=). For the sake of anyone looking for that technique, here's how to do that:

content.split(/(?=[?.!])/)

# Returns an array with:
# ["Do you like to code", "? How I love to code", "! I'm always coding", "."]

A better example to illustrate the behavior is:

content = "- the - quick brown - fox jumps"
content.split(/(?=-)/)

# Returns an array with:
# ["- the ", "- quick brown ", "- fox jumps"]

Notice that the square bracket capture group wasn't necessary since there is only one delimiter. Also, since the first match happens at the first character it ends up as the first item in the array.

like image 85
sawa Avatar answered Oct 06 '22 09:10

sawa


To answer the question's title, adding a capture group to your split regex will preserve the split delimiters:

"Do you like to code? How I love to code! I'm always coding.".split /([?!.])/
  => ["Do you like to code", "?", " How I love to code", "!", " I'm always coding", "."]

From there, it's pretty simple to reconstruct sentences (or do other massaging as the problem calls for it):

s.split(/([?!.])/).each_slice(2).map(&:join).map(&:strip)
 => ["Do you like to code?", "How I love to code!", "I'm always coding."]

The regexes given in other answers do fulfill the body of the question more succinctly, though.

like image 45
Chris Heald Avatar answered Oct 06 '22 07:10

Chris Heald