Article Listing 1 Listing 2 mar2006.tar

Listing 2 escape_perl_regular_expression

#!/usr/bin/perl
# This program is licensed under the terms of the GNU 
# General Public License (GPL). 
# This program is a copyrighted by Neptune Web, Inc.
# Please send improvements and changes to 
# charles.dalsass@neptuneweb.com

$s = '';
while (<STDIN>) {
$s .= $_;
} 

print removeWhiteSpaceAndEscapeRegexp($s);


sub removeWhiteSpaceAndEscapeRegexp() {
    my ($ins) = @_;
# remove all regexp meaningful characters.
    $ins =~ s/([\$\|\*\?\]\[\^\/\+\.\"\(\)])/\\$1/isg;
    # add whitespace independence, but only after weird 
    # (regular expression) chars have been removed.
    $ins =~ s/\s+/\\s*/isg;
    return $ins;
}