我试图用带括号的正则表达式包围一些文本.例如,将所有内容替换is为(is):
Input is : This is a long sentence that IS written.
Desired output: This (is) a long sentence that (IS) written.
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?(虽然仍然保持找到的字符串的原始案例)
irb(main):001:0> s = 'This is a long sentence that IS written.'
=> "This is a long sentence that IS written."
irb(main):002:0> s.gsub(/\bis\b/i, '(\0)')
=> "This (is) a long sentence that (IS) written"
irb(main):003:0> s
=> "This is a long sentence that IS written"
irb(main):004:0> s.gsub!(/\bis\b/i, '(\0)')
=> "This (is) a long sentence that (IS) written"
irb(main):005:0> s
=> "This (is) a long sentence that (IS) written"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2278 次 |
| 最近记录: |