I am coding a script in Perl and need to extract some information from a text file.
This is what my code looks like - the string values are made up but represent all possible string variations.
my @alpha = ("abcdefgh(i) jklmno(pqrs3), uvwxyz",
"abcdefghi jklmn(opq1st), uvwxyz",
"abcdefghi jklmn(o_q(1s3)), uvwxyz",
"abcdef(gh)i jklmno(pq(1s3)), uvwxyz");
foreach my $line (@alpha){
if ($line =~ /\((.*\(?.*\)?)\),/){
print $1
}
}
I am trying to capture the large text between the last set of parenthesis (or brackets for us British English speakers).
Please note I am using the "dot" operator since I want to match anything, text, numbers, or other special characters.
Essentially I want to print out:
pqrs3
opq1st
o_q(1s3)
pq(1s3)
But I keep getting:
(i) jklmno(pqrs3) <-- not ok
opq1st <-- this is ok
opq(1s3) <-- this is also ok
gh)i jklmno(pq(1s3) <-- not ok
What am I doing wrong? or is it even possible to match this way?
Any help is appreciated.