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

标签 regex perl quotes

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

$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/

相关文章:

mysql - 使用 MySQL 和 Perl 随机化维基百科中的页面?

perl - LWP::UserAgent 请求方法的真正超时

mysql - 何时在 MySQL 中使用单引号、双引号和反引号

java - 在JAVA中使用REGEX从方法调用中提取参数

javascript - jQuery 验证方法 (RegEx)

java - 正则表达式匹配给定序列的空白、任何字符和/或换行符

linux - 如何将文件内容分段打印到屏幕上?

PHP/jQuery - 单引号在 Ajax 调用中加倍

php - SQL 引用在一台服务器上产生 sql 语法错误,而不是另一台服务器

c# - 在 .Net 中查找并替换 JSON 响应中的字符串