gpt4 book ai didi

regex - Perl 正则表达式删除引号之间的逗号?

转载 作者:行者123 更新时间:2023-12-02 21:15:17 25 4
gpt4 key购买 nike

我正在尝试删除字符串中双引号之间的逗号,同时保留其他逗号不变? (这是一个电子邮件地址,有时包含多余的逗号)。下面的“暴力”代码在我的特定机器上工作正常,但是有没有更优雅的方法来做到这一点,也许使用单个正则表达式?邓肯

$string = '06/14/2015,19:13:51,"Mrs, Nkoli,,,ka N,ebedo,,m" <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd889f9c9f9c9396929b9b949e98c4cebd9a909c9491d39e9290" rel="noreferrer noopener nofollow">[email protected]</a>>,1,2';
print "Initial string = ", $string, "<br>\n";

# Extract stuff between the quotes
$string =~ /\"(.*?)\"/;

$name = $1;
print "name = ", $1, "<br>\n";
# Delete all commas between the quotes
$name =~ s/,//g;
print "name minus commas = ", $name, "<br>\n";
# Put the modified name back between the quotes
$string =~ s/\"(.*?)\"/\"$name\"/;
print "new string = ", $string, "<br>\n";

最佳答案

您可以使用这种模式:

$string =~ s/(?:\G(?!\A)|[^"]*")[^",]*\K(?:,|"(*SKIP)(*FAIL))//g;

图案详细信息:

(?: # two possible beginnings:
\G(?!\A) # contiguous to the previous match
| # OR
[^"]*" # all characters until an opening quote
)
[^",]* #"# all that is not a quote or a comma
\K # discard all previous characters from the match result
(?: # two possible cases:
, # a comma is found, so it will be replaced
| # OR
"(*SKIP)(*FAIL) #"# when the closing quote is reached, make the pattern fail
# and force the regex engine to not retry previous positions.
)

如果您使用较旧的 Perl 版本,则可能不支持 \K 和回溯控制动词。在这种情况下,您可以将此模式与捕获组一起使用:

$string =~ s/((?:\G(?!\A)|[^"]*")[^",]*)(?:,|("[^"]*(?:"|\z)))/$1$2/g;

关于regex - Perl 正则表达式删除引号之间的逗号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31148586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com