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 (?<=')但沒有運氣。
uj5u.com熱心網友回復:
您可以使用
sample_string.scan(/(?<![\w'])\w /)
sample_string.scan(/\b(?<!')\w /)
請參閱Rubular 演示。模式(它們是絕對同義詞)匹配
(?<![\w'])- 字串中沒有緊跟在單詞或'字符前面的位置\b(?<!')- 一個字邊界位置,前面沒有一個'字符\w- 一個或多個字字符。
請參閱Ruby 演示:
sample_string = "let's could've they'll you're won't"
p sample_string.scan(/(?<![\w'])\w /)
# => ["let", "could", "they", "you", "won"]
uj5u.com熱心網友回復:
鑒于:
> 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"]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/314391.html
