[英]Perl iterate over the number of regexp matches
I have some lines like我有一些像
int foo[] = { a, b, c, d, e };
The exact numbers of elements in the array is unknown.数组中元素的确切数量是未知的。 I want to match it using
perl
's regexp.我想使用
perl
的正则表达式来匹配它。 I identify the lines using我使用识别线
if ( $line =~ /^\ *int\ *([a-z]+)\ *\[\]\ *=\ *{(\ *([a-z]+)\ *,?)+}\ *;/ )
{
print $line;
}
How can I get the number of matches, as I want to work on each of the elements in the array?当我想处理数组中的每个元素时,如何获得匹配的数量?
Ideally, I would like something like理想情况下,我想要类似的东西
if ( $line =~ /^\ *int\ *([a-z]+)\ *\[\]\ *=\ *{(\ *([a-z]+)\ *,?)+}\ *;/ )
{
my $count_of_matches = ??????
for (my $matches=0 ; $matches<count_of_matches ; $matches++)
{
print $matches, "\n";
}
}
which should result in something like这应该会导致类似
foo
a
b
c
d
e
I tried我试过了
my $count_of_matches = () = $line =~ /.../ # using the same pattern as above
my @list = $line =~ /.../
my $other_count_of_matches = scalar @list
But I get a count of 3, and the perl
array only contains the array name ( foo
) and twice the last element ( e
).但我得到的计数为 3,并且
perl
数组仅包含数组名称 ( foo
) 和最后一个元素 ( e
) 的两倍。
Do it in two steps.分两步进行。 First, match the whole contents of
{...}
, then use split to retrieve the individual elements.首先,匹配
{...}
的全部内容,然后使用split检索各个元素。
BTW, you don't have to backslash spaces (unless you use the /x
modifier).顺便说一句,您不必使用反斜杠空格(除非您使用
/x
修饰符)。
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
my $string = 'int foo[] = { a, b, c, d, e };';
my ($array, $elements) = $string =~ /^ *int *([a-z]+) *\[\] *= *\{ *((?:[a-z]+ *,? *)+)\};/;
my @elements = split /[, ]+/, $elements;
say "<$_>" for $array, @elements;
When you find regexes too limiting, use a real C parser.当您发现正则表达式过于受限时,请使用真正的 C 解析器。
use MarpaX::Languages::C::Scan qw();
my $ast = MarpaX::Languages::C::Scan->new(
content => 'int foo[] = { a, b, c, d, e };'
)->ast;
for my $id (
$ast->findnodes('//declarator//IDENTIFIER'),
$ast->findnodes('//initializerList//IDENTIFIER')
) {
say $id->getAttribute('text');
}
__END__
foo
a
b
c
d
e
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.