0

我有这些字符串:

A regular sentence.
A regular sentence (United Kingdom).
A regular sentence (UK).

目标是删除括号中的术语,因此所需的输出将是:

A regular sentence.
A regular sentence.
A regular sentence.

如何在 Ruby 中实现这一点(可能使用正则表达式?)?

谢谢

4

3 回答 3

3

这应该有效:

string.gsub(/\s*\(.*\)/, '')
于 2013-10-23T10:28:34.350 回答
0
"A regular sentence (UK).".gsub(/\(.*\)/,"").strip #=> "A regular sentence ."
于 2013-10-23T11:18:54.400 回答
0

如果句子本身可以包含括号:

a = "A (very) regular sentence (UK)."
p a.gsub(/\s\([^()]*\)(?=\.\Z)/, '') #=> "A (very) regular sentence."
于 2013-10-23T17:55:07.617 回答