我有这些字符串:
A regular sentence.
A regular sentence (United Kingdom).
A regular sentence (UK).
目标是删除括号中的术语,因此所需的输出将是:
A regular sentence.
A regular sentence.
A regular sentence.
如何在 Ruby 中实现这一点(可能使用正则表达式?)?
谢谢
我有这些字符串:
A regular sentence.
A regular sentence (United Kingdom).
A regular sentence (UK).
目标是删除括号中的术语,因此所需的输出将是:
A regular sentence.
A regular sentence.
A regular sentence.
如何在 Ruby 中实现这一点(可能使用正则表达式?)?
谢谢
这应该有效:
string.gsub(/\s*\(.*\)/, '')
"A regular sentence (UK).".gsub(/\(.*\)/,"").strip #=> "A regular sentence ."
如果句子本身可以包含括号:
a = "A (very) regular sentence (UK)."
p a.gsub(/\s\([^()]*\)(?=\.\Z)/, '') #=> "A (very) regular sentence."