文字列の末尾から照合を行う必要がある場合に、Ruby で貪欲でない正規表現を使用することについて混乱しています。
私の文字列:
s = "Some words (some nonsense) and more words (target group)"
そして、結果に「(ターゲットグループ)」を取得したい。どうやってやるの?次のことを試していました:
よく深い:
s.match(/\(.*\)$/)[0]
=> "(some nonsense) and more words (target group)"
s.match(/\(.*\)/)[0]
=> "(some nonsense) and more words (target group)"
非貪欲:
s.match(/\(.*?\)/)[0]
=> "(some nonsense)"
s.match(/\(.*?\)$/)[0]
=> "(some nonsense) and more words (target group)"
最初の文字列には、「()」内に任意の数のグループが含まれる場合と含まれない場合があることに注意してください。