gpt4 book ai didi

regex - 提取逗号和引号之间的文本,忽略 perl 中的转义字符

转载 作者:行者123 更新时间:2023-12-01 10:36:27 28 4
gpt4 key购买 nike

我需要分隔以下字符串:"ABC", err, "%. Exiting.", "使用选项\"-a,-a,-s,-t\"或\"-aa,-bb\".",0,-1,""进入逗号和引号之间的字符。我在 perl 中使用以下正则表达式:\(".*?"|[^",]+)(?=\s*,|\s*$)\g。这给出了我得到以下输出,其中标识了一个额外的空间。

"ABC"
err
"%. Exiting."
#[Unwanted Match]
"Use options \"-a,-a,-s,-t\" OR \"-aa,-bb\"."
0
-1
"" #[This Match is required]

我怎样才能忽略这个空格,在最后用引号引号保留空字符串

最佳答案

不要使用正则表达式。虽然他们可以做到,但您正在匹配嵌套的东西,这充其量只会使正则表达式变得非常困惑和复杂。使用解析器。像 Text::ParseWords:

#!/usr/bin/env perl
use strict;
use warnings;

use Text::ParseWords;

my $line = '"ABC", err, "%. Exiting." , "Use options \"-a,-a,-s,-t\" OR \"-aa,-bb\".",0,-1,""';
my @words = parse_line( ',', 1, $line );
print join ("\n", @words );

给出:

"ABC"
err
"%. Exiting."
"Use options \"-a,-a,-s,-t\" OR \"-aa,-bb\"."
0
-1
""

关于regex - 提取逗号和引号之间的文本,忽略 perl 中的转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34588430/

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