Link to home
Start Free TrialLog in
Avatar of trevor1940
trevor1940

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trevor1940
trevor1940

ASKER

Thanx
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/) {
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/.../).
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
OK, thanks Trev.