我们可以在gsub中使用关系运算符吗?
我需要将 .
字符替换为 。 \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"
约束是 => “规格重力= 1.015”
。
I need to replace the .
character with . \n
in the following string format. But, the constraint is, don't replace the .
character with .\n
in following pattern string only.
"test was done and was negative. Urine dipstick: ph = 6\\n \\342\\200\\242 spec. Grav. = 1.015"
I need the following output, like
"test was done and was negative. \n Urine dipstick: ph = 6\\n \\342\\200\\242 spec. Grav. = 1.015"
The constraint is => "spec. Grav. = 1.015"
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
String.sub 仅替换第一个匹配项。
String.sub only substitutes the first match.
应该做这项工作。
简要说明
\.
匹配任何.
(?!)
表示否定前瞻。这意味着它不会与这些括号中找到的任何内容匹配。(Grav| =)
因此,后跟Grav
或=
的点将不会匹配。should do the job.
Brief explanation
\.
matches any.
(?!)
denotes a negative look-ahead. That means that it won't match anything found in these brackets.(Grav| =)
hence a dot followed by eitherGrav
or=
won't be matched.你想要这个吗?
You want this?