perl: REGEX with a # in the pattern


Can anyone see why this errors on the regex?
I'm assuming it's to do with the '#'

use strict; use warnings;
use HTML::TreeBuilder;
use HTML::Element;

my $Base = "H:\\Path\\To\\";
my $Sub = "Sub Folder needs changing 123456 ";

my $body =HTML::TreeBuilder->new_from_file(*DATA);
my @DIVs =  $body -> look_down('_tag', 'div');  
for my $div (@DIVs){
    my $id = $div->attr('id');
    if((defined($id)) && ($id=~m/post_message_(\d.*)/)  ){
        my $PN = $1;
        print "ID = $id PN = $PN\n";
        my $Fold = $Base . $Sub . $PN;
        if(-d "$Fold"){
            print "HeLlo $Fold\n";
              print "Can''t find $Fold\n";
        my @font = $div-> look_down('_tag', 'font', 'color', 'red');
        for my $ft (@font){
        my $FText = $ft ->as_trimmed_text;

### this errors with Global symbol "$FTest" requires explicit package name at 
        if($FTest =~ m/(.*\#\d*)\s/  ){
            $FText =$1;
        print "FText = $FText\n";
print "Finished \n";

<div class="postbody">
<div class="postrow has_after_content">
<div class="content">
<div id="post_message_30829574">
<blockquote class="postcontent restore ">
<div style="text-align: center;"><i><b><font color="red"><font size="5">I want this #123456 but not this</font></font></b></i><br />
<br />

Open in new window

Who is Participating?
wilcoxonConnect With a Mentor Commented:
It has nothing to do with the regex.  You appear to have made a typo on that line - $FTest should be $FText (x instead of s).

Edit - you may have an issue with your regex.  Do you want "I want this #12345" or just "#12345"?  Your regex will give you the former.  If you just want #12345, you can do:
if ($FText =~ m/.*(\#\d*)\s/) {

Open in new window

One minor thing I would suggest is to use m{...} instead of m/.../ - it's more versatile.
trevor1940Author Commented:
Stupid Error i'd been starring at that for ages
Why do people use this:
    if ($FText =~ m/.*(\#\d*)\s/) {
instead of doing it without the "m", like this?:
    if ($FText =~ /.*(\#\d*)\s/) {
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

I can't speak for anyone else but I almost always use m{...} for all of its advantages - only on very short temporary scripts do I use /.../ (and never m/.../).
trevor1940Author Commented:
When first learning perl I was taught to use
$FText =~ m/.*(\#\d*)\s/

Open in new window

Now it's Habit

To m or not to m
Take this
my $text = "abc123def";

if($text =~ m{(\d*)})
if($text =~ m/(\d*)/)
$text =~ s/\d*//;
$text =~ s{\d*}{};

Open in new window

To me using the 'm' I instantly know I'm matching and not typo

It comes down to personal preference  I guess
OK, thanks Trev.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.