perl: REGEX with a # in the pattern

Hi

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";
            }
        else{
              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";

__DATA__
<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

LVL 1
trevor1940Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wilcoxonCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trevor1940Author Commented:
Thanx
Stupid Error i'd been starring at that for ages
0
tel2Commented:
Why do people use this:
    if ($FText =~ m/.*(\#\d*)\s/) {
instead of doing it without the "m", like this?:
    if ($FText =~ /.*(\#\d*)\s/) {
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

wilcoxonCommented:
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/.../).
0
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*)})
OR
if($text =~ m/(\d*)/)
and
$text =~ s/\d*//;
or
$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
0
tel2Commented:
OK, thanks Trev.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.