【问题标题】:Removing text within parentheses and trailing spaces删除括号内的文本和尾随空格
【发布时间】:2017-05-31 17:25:48
【问题描述】:

我为此使用 Rubular。我有以下字符串:

summary = "Hi world. Hi world. Hi world. Hi world. Hi world. Hi world. Hi world. Hi world (this is here). Hi world Hi world (wow)."

我正在尝试使用以下内容从字符串中删除所有括号:

summary.gsub!(/\([^()]*\)/,"")

问题是这不是抢空间,所以结果如下:

“嗨,世界。嗨,世界。嗨,世界。嗨,世界。嗨,世界。嗨,世界。嗨,世界。嗨,世界。嗨,世界,嗨,世界。”

注意句号前的多余空格。如何更新正则表达式以删除删除括号时留下的额外空间?

【问题讨论】:

  • 使用summary.gsub!(/\s*\([^()]*\)/,"")
  • 顺便说一句,(Remove) This text is ok (remove). 呢?

标签: ruby regex rubular


【解决方案1】:

如果有的话,它实际上只是一个小的修改,使其也可以捕获空格:

summary.gsub!(/\s*\([^\)]*\)/, '')

这只会捕获前导空格。如果你想要前导和尾随:

summary.gsub!(/\s*\([^\)]*\)\s*/, '')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多