gpt4 book ai didi

regex - 使用 scan 方法 + regexp 将字符串分解为单词,如果单词有 `' ` 字符,则删除该字符及其后的所有内容

转载 作者:行者123 更新时间:2023-12-04 14:02:29 24 4
gpt4 key购买 nike

sample_string = "let's could've they'll you're won't"
sample_string.scan(/\w+/)

上面给了我:

["let", "s", "could", "ve", "they", "ll", "you", "re", "won", "t"]

我想要的:

["let", "could", "they", "you", "won"]

一直在玩 https://rubular.com/并尝试像 \w+(?<=') 这样的断言但没有运气。

最佳答案

给定:

> sample_string = "let's could've they'll you're won't"

您可以进行拆分和映射:

> sample_string.split.map{|w| w.split(/'/)[0]}
=> ["let", "could", "they", "you", "won"]

关于regex - 使用 scan 方法 + regexp 将字符串分解为单词,如果单词有 `' ` 字符,则删除该字符及其后的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69559686/

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