EXPS我需要写正则表达式,将解析字符串是这样的:关于产品REG在Perl
Build-Depends: cdbs, debhelper (>=5), smthelse
我想提取包名(不包括版本号和括号内)。
我写了这样的事情:
$line =~ /^Build-Depends:\s*(\S+)\s$/
但它不正是我想要的。
有人知道如何管理它吗?
P.S.我只是想获得名单:“CDBS debhelper的smthelse”结果
EXPS我需要写正则表达式,将解析字符串是这样的:关于产品REG在Perl
Build-Depends: cdbs, debhelper (>=5), smthelse
我想提取包名(不包括版本号和括号内)。
我写了这样的事情:
$line =~ /^Build-Depends:\s*(\S+)\s$/
但它不正是我想要的。
有人知道如何管理它吗?
P.S.我只是想获得名单:“CDBS debhelper的smthelse”结果
此正则表达式应该做你想要什么:/\s(\S*)(?:\s\(.*?\))?(?:,|$)/g
编辑:你会从所有的结果称呼它循环:
while ($str =~ /\s(\S*)(?:\s\(.*?\))?(?:,|$)/g) {
print "$1 is one of the packages.\n";
}
这将适用于此处列出的软件包名称的类型。
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;
如果(
不存在,如何将输入分割为空格并打印每个元素? 也许这样的东西
perl -lane 'foreach $_ (@F[1..scalar(@F)]) {print if not m/\(/}'
cdbs,
debhelper
smthelse
它捕获什么?它与你想要的有什么不同?你的字符串在输入字符串的末尾是否总是有空格? – codaddict
我只想得到清单:“cdbs debhelper smthelse” – Nikita