正则表达式并用 preg_replace 替换双撇号
我有几行需要更新,其中双撇号在某些位置被替换并被删除,但其他位置则没有。
所以:
(2, 'Name 2', '', 8, 0, 0, 1, 'Info blah blah', 0, 4),
(3, 'Name 3', 'A normal bit of information', 8, 1, 0, 1, 'Info more blah', 0, 4),
(45, 'Name 45', 'Info with '' in it like it''s stuff', 356, 10, 1, 1, '', 0, 9)
需要变成:
(2, 'Name 2', '', 8, 0, 0, 1, 'Info blah blah', 0, 4),
(3, 'Name 3', 'A normal bit of information', 8, 1, 0, 1, 'Info more blah', 0, 4),
(45, 'Name 45', 'Info with \'\' in it like it\'\'s stuff', 356, 10, 1, 1, '', 0, 9)
当尝试各种方法时,我设法用 \'\' 更新所有 '' ,然后破坏稍后使用的功能。
I have several lines that need to be updated where double apostrophes get replaced in some locations and slashed out but not others.
So :
(2, 'Name 2', '', 8, 0, 0, 1, 'Info blah blah', 0, 4),
(3, 'Name 3', 'A normal bit of information', 8, 1, 0, 1, 'Info more blah', 0, 4),
(45, 'Name 45', 'Info with '' in it like it''s stuff', 356, 10, 1, 1, '', 0, 9)
Needs to become:
(2, 'Name 2', '', 8, 0, 0, 1, 'Info blah blah', 0, 4),
(3, 'Name 3', 'A normal bit of information', 8, 1, 0, 1, 'Info more blah', 0, 4),
(45, 'Name 45', 'Info with \'\' in it like it\'\'s stuff', 356, 10, 1, 1, '', 0, 9)
When trying various methods I manage to update all '' with \'\' which then breaks functions used later on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该能够被
'$1\'\'$4'$5
替换,并且仅匹配单引号内的 2 个单引号,尽管后面是否出现逗号。This should be able to be replaced by
'$1\'\'$4'$5
and will match only 2 single quotes within single quotes despite if a comma occurs afterward in the literal.s/(?<=')([^',]*)''(?=[^',]*')/$1\\'\\'/g
记住,你稍后无法更改游戏并允许在分隔符“(')”之间使用单个撇号,因为它与“('')”不兼容。好的?
输出:
(2, '名称 2', '', 8, 0, 0, 1, '信息等等', 0, 4),
(3, '名称 3', '普通信息', 8, 1, 0, 1, '更多信息', 0, 4),
(45, '名称 45', '信息中带有 '' 就像它的东西', 356, 10, 1, 1, '', 0, 9)
<代码>==> (45, '名称 45', '其中包含“类似内容”的信息', 356, 10, 1, 1, '', 0, 9)
''''' ','''',''''
<代码>==> '\'\'\'\' ','\'\'','\'\''
s/(?<=')([^',]*)''(?=[^',]*')/$1\\'\\'/g
Remember, you can't change the game later and allow a single apostrophe between delimeters '(')', because that is not compatable with ' ('') '. Ok?
Output:
(2, 'Name 2', '', 8, 0, 0, 1, 'Info blah blah', 0, 4),
(3, 'Name 3', 'A normal bit of information', 8, 1, 0, 1, 'Info more blah', 0, 4),
(45, 'Name 45', 'Info with '' in it like it''s stuff', 356, 10, 1, 1, '', 0, 9)
==> (45, 'Name 45', 'Info with \'\' in it like it\'\'s stuff', 356, 10, 1, 1, '', 0, 9)
''''' ','''',''''
==> '\'\'\'\' ','\'\'','\'\''
嗯,这确实需要一些解析。如果您使用正则表达式,它实际上只能在最佳匹配的基础上工作。
如果您可以假设
'',
始终是 CSV 列表中的空字符串,则可以选择查找逗号。然而,如果其中一个字符串在双引号后包含逗号,那么这将会失败:为了添加一些安全性,您可以添加前缀检查,例如
(?<=[(\s])
-但这没什么帮助。Uhm, this really requires some parsing. If you use regular expressions it will really only work on a best bet basis.
If you can assume that
'',
is always the empty string in your CSV list, then looking out for the comma is an option. Should one of the strings however contain a comma after the double quote then this is going to fail:To add some safety you can add a prefix check like
(?<=[(\s])
- but that helps only little.