我需要编写正则表达式来解析字符串,如下所示:
Build-Depends: cdbs, debhelper (>=5), smthelse 我想提取包名称(没有版本号和括号)。
我写了这样的东西:
$line =~ /^Build-Depends:\s*(\S+)\s$/ 但这并不是我想要的。
有人知道如何管理它吗?
另外,我只想得到列表:"cdbs debhelper smthelse“的结果
发布于 2011-06-27 19:27:58
这个正则表达式应该可以执行您想要的操作:/\s(\S*)(?:\s\(.*?\))?(?:,|$)/g
编辑:您可以这样调用它来遍历所有结果:
while ($str =~ /\s(\S*)(?:\s\(.*?\))?(?:,|$)/g) {
print "$1 is one of the packages.\n";
}发布于 2011-06-27 19:28:26
使用正则表达式/^Build-Depends:\s*(\S+)\s$/,您将一直匹配到字符串的末尾。请尝试使用/^Build-Depends:\s*(\S+)\s/。
发布于 2011-06-27 19:46:03
这将适用于这里列出的包名的类型。
use warnings;
use strict;
my @packs;
my $line = "Build-Depends: cdbs, debhelper (>=5), smthelse";
if ( $line =~ /^Build-Depends: (.+)$/ ) { # get everything
@packs = split /,+\s*/, $1;
s/\([^)]+\)//g for @packs; # remove version stuff
}
print "$_\n" for @packs;https://stackoverflow.com/questions/6492076
复制相似问题