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

后端 未结 5 614
慢半拍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.

提交回复
热议问题