gpt4 book ai didi

Ruby 字符串分割成忽略所有特殊字符的单词 : Simpler query

转载 作者:太空宇宙 更新时间:2023-11-03 17:16:11 25 4
gpt4 key购买 nike

我需要在所有使用非单词字符的地方将查询拆分为单词。例如:

query = "I am a great, boy's and I like! to have: a lot-of-fun and @do$$nice&acti*vities+enjoy good ?times."

应该输出:

["I", "am", "a", "great", "", "boy", "s", "and", "I", "like", "", "to", "have", "", "a", "lot", "of", "fun", "and", "", "do", "", "nice", "acti", "vities", "enjoy", "good", "", "times"] 

这可以解决问题,但有更简单的方法吗?

query.split(/[ ,'!:\\@\\$\\&\\*+?.-]/)

最佳答案

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"]

这与预期的输出不同,因为它不包含空字符串。

关于Ruby 字符串分割成忽略所有特殊字符的单词 : Simpler query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19509635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com