How to backreference in Ruby regular [removed]regex) with gsub when I use grouping?

后端 未结 3 1666
花落未央
花落未央 2020-12-14 07:00

I would like to patch some text data extracted from web pages. sample:

t=\"First sentence. Second sentence.Third sentence.\"

There is no sp

3条回答
  •  無奈伤痛
    2020-12-14 07:48

    You can backreference in the substitution string with \1 (to match capture group 1).

    t = "First sentence. Second sentence.Third sentence!Fourth sentence?Fifth sentence."
    t.gsub(/([.!?])([A-Z1-9])/, "\\1\n\\2") # => "First sentence. Second sentence.\nThird sentence!\nFourth sentence?\nFifth sentence."
    

提交回复
热议问题