I'm fairly new to Perl (I know other languages), but I am extremely new to regular expressions. While writing a script, I ran into a little problem while trying to backreference to a match in a regular expression. The part of the code was similar to the following:
if(/(hello)/)
{
$foo = $1;
if(/(blah)/)
{
$bar = $1;
}
}
print "$foo and $bar";
What I thought before I ran into this problem was that the output would look like..
hello and blah
But I found out that the output is actually (assuming that my problem isn't due to some logical error that I've yet to notice)..
blah and blah
I don't really understand why this happens, and what I think right now is that the backreference variables ($1, $2, $3, etc..) are some sort of special reference variables that automatically dereference themselves when you use them. I'm not even sure if that makes sense, so I was wondering why the above situation occurs and if there is anyway to bypass that problem (without rewriting the regular expression, which I'm sure would be much easier
if(/(hello)/)
{
$foo = $1;
if(/(blah)/)
{
$bar = $1;
}
}
print "$foo and $bar";
What I thought before I ran into this problem was that the output would look like..
hello and blah
But I found out that the output is actually (assuming that my problem isn't due to some logical error that I've yet to notice)..
blah and blah
I don't really understand why this happens, and what I think right now is that the backreference variables ($1, $2, $3, etc..) are some sort of special reference variables that automatically dereference themselves when you use them. I'm not even sure if that makes sense, so I was wondering why the above situation occurs and if there is anyway to bypass that problem (without rewriting the regular expression, which I'm sure would be much easier