I want to find all calls of a function in a source-file using perl and regex.
The function call looks (simplified) like:
gettrans(ABC,DEF,GHI);
The second and third parameter are optional. So gettrans(ABC) and gettrans(ABC,DEF) are also valid. But the function must have at least the first parameter.
I'm using the following testfile to check my perl-code:
gettrans(ABC);
gettrans(ABC,D);
gettrans(ABC,D,E);
gettrans(A,B,C); gettrans(D);
gettrans(ABC,);
gettrans(,A);
My current perl-code looks as follows:
#!/usr/bin/perl -w
my $i = 1;
while ( my $line = <> ) {
my @res = ( $line =~ /gettrans\((\w+)(,\w+){0,2}\)/g );
print "line " . $i . ":\n";
foreach (@res) {
if ( defined($_) ) {
print $_ . "\n";
}
}
print "\n";
$i++;
}
This gives the following output:
line 1:
ABC
line 2:
ABC
,D
line 3:
ABC
,E
line 4:
A
,C
D
line 5:
line 6:
However, what I expected as output or better, what I want to have is something like:
line 1:
gettrans(ABC)
line 2:
gettrans(ABC,D)
line 3:
gettrans(ABC,D,E)
line 4:
gettrans(A,B,C)
gettrans(D)
line 5:
line 6:
Additionally, what is also very confusing for me is, that if I replace the commas in the testfile and in the regular expression with a 0 (zero), I get a total different output, that comes closer to what I want (except the fact, that in this case I also get output for line 5 and line 6 [which are invalid functions calls]).
Output for replacing commas with 0 (zero):
line 1:
ABC
line 2:
ABC0D
line 3:
ABC0D0E
line 4:
A0B0C
D
line 5:
ABC0
line 6:
0A
I'm just started learning both pearl and regex. Would be nice to get any hints about the misunderstandings I have at the moment.
Thank you all!
cu, peter
\w+.\wis equivalent with[a-zA-Z0-9_], so those zeros simply get matched as if they are letters like yourABCD.