我需要使用以下字符串格式将.
字符替换为. \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"
。
答案 0 :(得分: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仅替换第一个匹配。
答案 1 :(得分:0)
str.gsub(/\.(?! (Grav| =))/, ".\n")
应该做的。
\.
匹配任何.
(?!)
表示负面预测。这意味着它与这些括号中的任何内容都不匹配。(Grav| =)
因此,Grav
或=
之后的点将无法匹配。答案 2 :(得分:0)
你想要这个吗?
str.gsub(/\.(?!\n)/, "\.\n")