Re: regex problem




Quoth Charlton Wilbur <cwilbur@xxxxxxxxxxxxxx>:
"BM" == Ben Morrow <ben@xxxxxxxxxxxx> writes:

BM> Quoth Charlton Wilbur <cwilbur@xxxxxxxxxxxxxx>:
>> >>>>> "s" == sstark <sstark@xxxxxxxxxx> writes:

BM> [ sstark's code was

BM> $line =~ s/^$prev//;

BM> ]

s> Why isn't it deleting the value of $prev in $line?

>> Because ^ doesn't do what you think it does, and it only works in
>> the code you have there out of pure luck and coincidence.

BM> Please explain further. ^ means 'match at the beginning of the
BM> string', unless /m is given, in which case it means 'match at
BM> the beginning of any line'. How is this not what the OP thought
BM> it meant?

Because the strings he wants are at the start of the strings he's
looking at purely by accident -- it's not part of his specification.

The relevant bits of the original code are

while($line =~ /^(.*?href\s*=\s*\")([^\"]+)(\".*)/i){
my $prev =$1;
# ...
$line =~ s/^$prev//;

so it's *not* coincidence that the string he's searching for is at the
beginning. If it wasn't we wouldn't have got here in the first place
(note the ^ at the start of the original match).

Ben

.



Relevant Pages