On 8/29/07, Dharshana Eswaran <[EMAIL PROTECTED]> wrote:
snip
> But i want to read only the elements of the Enum and the values assigned to
> those elements. I dont want to read the Enum names and the extra characters.
> I am unable to filter them.
snip
Sometimes one regex is not enough. The following code has at least
one bug: it doesn't correctly handle enums of characters where one of
the characters is ',' (a valid C construct, but on I don't see often).
#!/usr/bin/perl
use strict;
use warnings;
my $text = "typedef enum foo {
LEV_0 = 0,
LEV_1,
LEV_2,
LEV_3,
LEV_4,
LEV_5
} sig;
typedef enum bar {
LEV_0 = 1,
LEV_1 = 2,
LEV_2=4,
LEV_3 = 8,
LEV_4 =16,
LEV_5 =32
} flag;";
my $ident = qr/ [A-Za-z_]\w+ /xs;
my $typedef = qr/ typedef \s+ /xs;
my $enum = qr/ $ident (?: \s* = \s* [^\s,]+)? /xs;
my $enum_statement = qr/
$typedef?
enum
\s+
($ident)
\s*
{
\s*
((?: $enum \s* ,? \s* )+)
\s*
}
/xs;
while ($text =~ /$enum_statement/g) {
my ($name, $block) = ($1, $2);
print "$name\n";
for my $e ($block =~ /($enum)/g) {
$e =~ s/\s*=\s*/ has a value of /;
print "\t$e\n";
}
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/