Ruby string split into words ignoring all special characters: Simpler query

前端 未结 2 1257
滥情空心
滥情空心 2021-01-16 08:46

I need a query to be split into words everywhere a non word character is used. For example:

query = \"I am a great, boy\'s and I like! to have: a lot-of-fun         


        
相关标签:
2条回答
  • 2021-01-16 09:24

    I am adding this answer as @sawa's did not exactly reproduce the desired output:

    #Split using any single non-word character:
    query.split(/\W/) #=> ["I", "am", "a", "great", "", "boy", "s", "and", "I", "like", "", "to", "have", "", "a", "lot", "of", "fun", "and", "", "do", "", "nice", "acti", "vities", "enjoy", "good", "", "times"]
    

    Now if you do not want the empty strings in the result just use sawa's answer.

    The result above will create many empty strings in the result if the string contains multiple spaces, as each extra spaces will be matched again and create a new splitting point. To avoid that we can add an or condition:

    # Split using any number of spaces or a single non-word character:
    query.split(/\s+|\W/)
    
    0 讨论(0)
  • 2021-01-16 09:25
    query.split(/\W+/)
    # => ["I", "am", "a", "great", "boy", "s", "and", "I", "like", "to", "have", "a", "lot", "of", "fun", "and", "do", "nice", "acti", "vities", "enjoy", "good", "times"]
    
    query.scan(/\w+/)
    # => ["I", "am", "a", "great", "boy", "s", "and", "I", "like", "to", "have", "a", "lot", "of", "fun", "and", "do", "nice", "acti", "vities", "enjoy", "good", "times"]
    

    This is different from the expected output in that it does not include empty strings.

    0 讨论(0)
提交回复
热议问题