计算 qr 正则表达式中的捕获组数量?
我正在开发一个项目,该项目有时会从 ftp 服务器获取文件列表。此时,它要么返回文件的 arrayref,要么如果传递一个可选的正则表达式引用(即 qr
),它会使用 grep 过滤列表。此外,如果该 qr 具有捕获组,它会将捕获的部分视为版本号,并返回一个 hashref,其中键是版本,值是文件名(将返回为如果没有捕获组,则为数组)。代码看起来像(稍微简化)
sub filter_files {
my ($files, $pattern) = @_;
my @files = @$files;
unless ($pattern) {
return \@files;
}
@files = grep { $_ =~ $pattern } @files;
carp "Could not find any matching files" unless @files;
my %versions =
map {
if ($_ =~ $pattern and defined $1) {
( $1 => $_ )
} else {
()
}
}
@files;
if (scalar keys %versions) {
return \%versions;
} else {
return \@files;
}
}
此实现尝试创建哈希并在成功时返回它。我的问题是,我是否可以检测到 qr
具有捕获组,并且只有在有捕获组时才尝试创建哈希?
I am working on a project which at one point gets a list of files from an ftp server. At that point it either returns an arrayref of files OR if an optional regex reference (i.e. qr
), is passed it filters the list down using grep. Further if that qr
has a capture group, it treats the captured section as a version number and returns instead a hashref where the keys are the versions and the values are the file names (which would have been returned as the array if no capture groups). The code looks like (simplified slightly)
sub filter_files {
my ($files, $pattern) = @_;
my @files = @$files;
unless ($pattern) {
return \@files;
}
@files = grep { $_ =~ $pattern } @files;
carp "Could not find any matching files" unless @files;
my %versions =
map {
if ($_ =~ $pattern and defined $1) {
( $1 => $_ )
} else {
()
}
}
@files;
if (scalar keys %versions) {
return \%versions;
} else {
return \@files;
}
}
This implementation tries to create the hash and returns it if it succeeds. My question, is can I detect that the qr
has a capture group and only attempt to create the hash if it does?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以使用类似的东西:
输出:
You could use something like:
Output:
请参阅
Regexp::Parser 中的
。nparen
避免检查模式的另一种可能性是简单地依赖
$capture
的值。如果成功匹配但未捕获,则为1
(Perl 真值)。您可以将其与偶尔返回1
的捕获区分开来,因为该捕获缺少IV
标志。See
nparen
inRegexp::Parser
.Another possibility to avoid inspecting the pattern is to simply rely on the value of
$capture
. It will be1
(Perl true value) in the case of a successful match without capture. You can distinguish it from the occasional capture returning1
because that one lack theIV
flag.您可以使用 YAPE::Regex 来解析正则表达式,看看是否有捕获存在:
You could use YAPE::Regex to parse the regular expression to see if there is a capture present: