使用 scan 方法 + regexp 将字符串分解为单词,如果单词有 `'` 字符,则删除该字符及其后的所有内容
Break string into words using scan method + regexp, if word has `'` character, drop this character and everything after it
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.scan(/(?<![\w'])\w+/)
sample_string.scan(/\b(?<!')\w+/)
见Rubular demo。模式(它们是绝对同义词)匹配
(?<![\w'])
- 字符串中未紧跟单词或 '
char 的位置
\b(?<!')
- 没有紧跟 '
char 的单词边界位置
\w+
- 一个或多个单词字符。
sample_string = "let's could've they'll you're won't"
p sample_string.scan(/(?<![\w'])\w+/)
# => ["let", "could", "they", "you", "won"]
鉴于:
> 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"]
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.scan(/(?<![\w'])\w+/)
sample_string.scan(/\b(?<!')\w+/)
见Rubular demo。模式(它们是绝对同义词)匹配
(?<![\w'])
- 字符串中未紧跟单词或'
char 的位置
\b(?<!')
- 没有紧跟'
char 的单词边界位置
\w+
- 一个或多个单词字符。
sample_string = "let's could've they'll you're won't"
p sample_string.scan(/(?<![\w'])\w+/)
# => ["let", "could", "they", "you", "won"]
鉴于:
> 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"]