0

我需要用以下字符串格式替换.字符。. \n但是,约束是,不要仅用以下模式字符串 替换.字符。.\n

"test was done and was negative. Urine dipstick: ph = 6\\n \\342\\200\\242 spec. Grav.  = 1.015"

我需要以下输出,例如

"test was done and was negative. \n Urine dipstick: ph = 6\\n \\342\\200\\242 spec. Grav.  = 1.015"

约束是 => "spec. Grav. = 1.015"

4

3 回答 3

1
str = "test was done and was negative. Urine dipstick: ph = 6\\n \\342\\200\\242 spec. Grav.  = 1.015"

puts str.sub('. ', ".\n") 

#=> test was done and was negative.
#=> Urine dipstick: ph = 6\n \342\200\242 spec. Grav.  = 1.015

String.sub 仅替换第一个匹配项。

于 2011-03-23T13:31:08.427 回答
0
str.gsub(/\.(?! (Grav| =))/, ".\n")

应该做的工作。

简要说明

  • \.匹配任何.
  • (?!)表示负前瞻。这意味着它不会匹配在这些括号中找到的任何内容。
  • (Grav| =)因此一个点后跟一个Grav=将不匹配。
于 2011-03-23T13:05:52.960 回答
0

你要这个?

str.gsub(/\.(?!\n)/, "\.\n")
于 2011-03-23T14:15:33.750 回答