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

后端 未结 5 588
慢半拍i
慢半拍i 2020-11-29 04:12

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 ?

相关标签:
5条回答
  • 2020-11-29 04:53

    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.

    0 讨论(0)
  • 2020-11-29 04:59

    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.

    0 讨论(0)
  • 2020-11-29 05:02

    The most robust way to do this is with a Natural Language Processing library: Rails gem to break a paragraph into series of sentences

    You can also split in groups:

    @content.split(/(\?+)|(\.+)|(!+)/)
    

    After splitting into groups, you can join the sentence and delimiter.

    @content.split(/(\?+)|(\.+)|(!+)/).each_slice(2) {|slice| puts slice.join}
    
    0 讨论(0)
  • 2020-11-29 05:08

    I'd use something like:

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

    If you want to get rid of the intervening spaces, use:

    content.scan(/.+?[?!.]/).map(&:lstrip)
    # => ["Do you like to code?", "How I love to code!", "I'm always coding."]
    
    0 讨论(0)
  • 2020-11-29 05:19

    Use partition. An example from the documentation:

    "hello".partition("l")         #=> ["he", "l", "lo"]
    
    0 讨论(0)
提交回复
热议问题