I just started learning perl about a week ago. But i have a basic knowledge of regex features like back-references, look arounds etc. So i wrote a small regex to match strings in an array (this array represents every line of a file) that has only '{' as a printable character.
my regex goes like:
for my $f_line (@file_lines) {
my @opening_brace;
if ($f_line =~ /(^[[:blank:]]*(?={))({[[:blank:]]*$)/) {
@opening_brace = $2;
print "opening brace : @opening_brace \n";
}
}
However, my regex couldn't get me into the if block even though it worked fine with grep when i tested it against the target file.
What am i doing wrong?
i tried:
echo "{ " | grep -P '(^[[:blank:]]*(?={))({[[:blank:]]*$)'
and got:
{
Oh I just found a fix. It appears my file 'lines' are not really lines. The problem lies in the subtle difference in logical lines of a string and literal anchors denoting the start and end of a file line which were created when I pushed the lines into an array as strings. Thanks for the help tho