I would like to match a pattern with perl that contains a hash (or pound character '#'):
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
if( $#ARGV + 1 != 2 ) {
die "ERROR: Number of command line arguments is not 2";
}
my $input = $ARGV[ 0 ];
my $output = $ARGV[ 1 ];
open( my $ifh, "<:encoding(UTF-8):crlf", "$input" )
|| die "ERROR: Failed to open input file: $!";
my $content = do { local $/; <$ifh> };
open( my $ofh, ">:encoding(UTF-8):crlf", "$output" )
|| die "ERROR: Failed to open output file: $!";
eval { # GLOBAL EXCEPTION HANDLER BEGIN
{ ( ( my $fix, my $dat, my $rest ) = ( $content
=~ / ^ ( \Q# started on \E ) ([^\n]+) \n{3}
(.*)
$ /xs
) ) || die "ERROR: No match";
print $ofh __LINE__ . "\tDate\t$dat\n";
$content = $rest; }
}; if( $@ ) { print $ofh "$@____________\n"; print $ofh "$content"; } # GLOBAL EXCEPTION HANDLER END
This gives the following error message:
Unmatched ( in regex; marked by <-- HERE in m/ ^ ( <-- HERE \#\ started\ on\ \\E\ \)\ \(\[\^\\n\]\+\)\ \\n\{3\}\
\ \ \ \ \ \ \ \(\.\*\)\
\ \ \ \ \ \$\ / at ./example.pl line 25.
However, if I change it like this, then it works perfectly:
{ ( ( my $fix, my $dat, my $rest ) = ( $content
=~ / ^ ( \# \Q started on \E ) ([^\n]+) \n{3}
(.*)
$ /xs
) ) || die "ERROR: No match";
print $ofh __LINE__ . "\tDate\t$dat\n";
$content = $rest; }
I would prefer, however, to keep the hashtag inside the \Q and \E block.
So the question is, does \Q and \E not escape everything inside it? Or what is wrong here?