正则表达式并用 preg_replace 替换双撇号

发布于 2024-10-16 21:44:49 字数 610 浏览 1 评论 0原文

我有几行需要更新,其中双撇号在某些位置被替换并被删除,但其他位置则没有。

所以:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

極樂鬼 2024-10-23 21:44:49
'(([^']*?)('{2})([^']*?))+'([,|\)])

这应该能够被 '$1\'\'$4'$5 替换,并且仅匹配单引号内的 2 个单引号,尽管后面是否出现逗号。

'(([^']*?)('{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.

花开柳相依 2024-10-23 21:44:49

s/(?<=')([^',]*)''(?=[^',]*')/$1\\'\\'/g

记住,你稍后无法更改游戏并允许在分隔符“(')”之间使用单个撇号,因为它与“('')”不兼容。好的?

use strict;
use warnings;

my @data = (
"(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)",
"''''' ','''',''''",
);

for (@data) {
    print "\n$_\n";
    if (
          s/ (?<=')([^',]*) '' (?= [^',]*')/$1\\'\\'/xg
       )
    {
       print "==>\t$_\n";
    }
}

输出:
(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?

use strict;
use warnings;

my @data = (
"(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)",
"''''' ','''',''''",
);

for (@data) {
    print "\n$_\n";
    if (
          s/ (?<=')([^',]*) '' (?= [^',]*')/$1\\'\\'/xg
       )
    {
       print "==>\t$_\n";
    }
}

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)
''''' ','''',''''
==> '\'\'\'\' ','\'\'','\'\''

隐诗 2024-10-23 21:44:49

嗯,这确实需要一些解析。如果您使用正则表达式,它实际上只能在最佳匹配的基础上工作。

如果您可以假设 '', 始终是 CSV 列表中的空字符串,则可以选择查找逗号。然而,如果其中一个字符串在双引号后包含逗号,那么这将会失败:

preg_replace("/''(?![,)])/", "\\'\\'", $text);

为了添加一些安全性,您可以添加前缀检查,例如 (?<=[(\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:

preg_replace("/''(?![,)])/", "\\'\\'", $text);

To add some safety you can add a prefix check like (?<=[(\s]) - but that helps only little.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文