Forum Moderators: coopster & phranque

Message Too Old, No Replies

Removing comments

Not working...

         

adni18

10:28 pm on Dec 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi. I want to use a regexp to remove everything in #'s. How can this be done?

Ex:


hello there #remove remove this, and this remove this#
don't remove anything until #now#

IanKelley

1:17 am on Dec 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$var =~ s/#[^#]+#//g;

kaled

1:19 am on Dec 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use the following to remove a comment from a line :-

$line =~ s/#.*//;

Kaled.

IanKelley

7:58 am on Dec 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That will remove everything after 'hello there'

wruppert

6:49 pm on Dec 22, 2004 (gmt 0)

10+ Year Member



$var =~ s/#[^#]+#//g;

This is too greedy, it consumes everything from the first # to the last one. Also, I don't think it will remove two adjacent #'s because of using + instead of *.

I would replace the [^#]+ with [^#]*?. The? causes the * quantifier to do a minimal match.

$var =~ s/#[^#]*?#//g;

IanKelley

9:19 am on Dec 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you had actually tested it, you'd know that's not true. ;-)

[^#]+
is incapable of matching anything after the first # it encounters.

wruppert

3:39 pm on Dec 23, 2004 (gmt 0)

10+ Year Member



Ian,

You're right, I was wrong about the greediness of that pattern. The '?' is not needed in this case.

However (after testing this time!), I did find that the + does not correctly handle empty comments (##), while the * does.


use strict;
use warnings;
my $var1 = "hello #remove this# don't remove ## until #now# ";
$var1 =~ s/#[^#]*#//g;
print $var1;
print "\n";
my $var2 = "hello #remove this# don't remove ## until #now# ";
$var2 =~ s/#[^#]+#//g;
print $var2;

Produces the output:

hello don't remove until
hello don't remove #now#

IanKelley

12:08 am on Dec 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, good point, if there's any chance of empty comments it's best to use * instead of +