Regex 从perl连接数组结果中删除元素
我的问题是:Regex 从perl连接数组结果中删除元素,regex,perl,Regex,Perl,我的问题是: @array = (possible text, text surrounded with round brackets, text without brackets, text surrounded with round brackets, possible text); $line = join(' ', @array); 如果连接结果可能文本的第一个括号之前有任何文本,最后一个括号之后有任何文
@array = (possible text,
text surrounded with round brackets,
text without brackets,
text surrounded with round brackets,
possible text);
$line = join(' ', @array);
如果连接结果可能文本的第一个括号之前有任何文本,最后一个括号之后有任何文本,我想删除。
谢谢
实数代码:
my (@lines, $line, $anchor, $left, $right, $parent, $elem);
($anchor) = $tree->look_down(_tag=>"span", class=>"txt");
if ($anchor) {
$elem = $anchor;
my ($product, @tmp);
while (($elem = $elem->right()) &&
((ref $elem) && ($elem->tag() ne "table"))) {
@tmp = get_all_text($elem);
push @lines, @tmp;
$line = join(' ', @tmp);
看看这是否适合您:
$line =~ s/.*?(\(.*\)).*/$1/;
您的代码有语法错误 您应该首先解决这个问题,然后找出如何进一步处理$line 也许你忘了给@array分配一个qw 如果是这样,那么下面的代码将在第一个文本之前剪切文本 并将最后一个文本后的文本包围
#!/usr/bin/perl
#use warnings;
#use strict;
@array = qw(possible text,
text surrounded with round brackets,
text without brackets,
text surrounded with round brackets,
possible text);
$line = join(' ', @array);
$line =~ s/.*?(text surrounded with round brackets)/$1/;
$line =~ s/(.*text surrounded with round brackets).*/$1/;
print "$line\n";
尝试:
可以在数组上循环,建立第一个括号和最后一个括号所在位置的索引,然后提取相应的切片
my @array = ('possible text',
'(text surrounded with round brackets)',
'text without brackets',
'(text surrounded with round brackets)',
'possible text');
my ($first, $last);
for (my $i = 0; $i < $#array; ++$i) {
next unless $array[$i] =~ m/^\s*\(/; # maybe adapt this regex
$first = $i;
last;
}
for (my $j = $#array; $j > 0; --$j) {
next unless $array[$i] =~ m/^\s*\(/; # tweak this too then
$last = $j;
last;
}
my $line = join (' ', @array[$first..$last]);
恐怕这不像地图或grep那么优雅
编辑:最初只有一个循环来查找$first和$last,但两个单独的循环更有效。这也取决于数据的结构;如果没有太多,这种优化显然不是很重要。另一方面,如果真的有很多数据,您可以进一步优化它。同样重要的是:发布示例输入数据和所需输出。
my @array = ('possible text',
'(text surrounded with round brackets)',
'text without brackets',
'(text surrounded with round brackets)',
'possible text');
my ($first, $last);
for (my $i = 0; $i < $#array; ++$i) {
next unless $array[$i] =~ m/^\s*\(/; # maybe adapt this regex
$first = $i;
last;
}
for (my $j = $#array; $j > 0; --$j) {
next unless $array[$i] =~ m/^\s*\(/; # tweak this too then
$last = $j;
last;
}
my $line = join (' ', @array[$first..$last]);