what's wrong in this Perl Regex expression?

A

Alont

$body is a html file waiting for process,
my purpose very simple----I want get the content of $body from
"<!--head-->" to "<!--bottom -->"

$body =~ {<!--head-->.*<!--bottom -->};
print $body;

the error record:
File::Glob object version 0.991 does not match bootstrap parameter
1.03 at F:/Perl/lib/XSLoader.pm line 95.

BTW: What's the string connect operator in Perl? for example:
there have $head, $body, $trailer and I want make them to one $
variable, if in VB I can worte: $all=$head & $body & trailer, I have
searched perlop - Perl operators and precedence Document, find
nothing about this
 
T

Tore Aursand

$body is a html file waiting for process, my purpose very simple----I
want get the content of $body from "<!--head-->" to "<!--bottom -->"

$body =~ {<!--head-->.*<!--bottom -->}; print $body;

Untested:

if ( $body =~ /(<!--head-->.*<!--bottom-->)/ ) {
my $content = $1;
}
else {
# Is something wrong?
}

If you don't want to use the 'if';

my ( $content ) = $body =~ /.../;
BTW: What's the string connect operator in Perl?

The '.' character.
there have $head, $body, $trailer and I want make them to one $
variable, if in VB I can worte: $all=$head & $body & trailer

Perl:

my $all = $head . $body . $trailer;
 
A

Alont

Tore Aursand said:
$body =~ /(<!--head-->.*<!--bottom-->)/

this expression always don't work in my Perl compiler,
and compiler don't give me error information,
I've tried this:
$text_start = "<div id="boxtitle">";
$text_end = "<div id="bottom">";
my $content =~ $body =~ /($text_start.*$text_end)/;
this also no result.
 
J

John W. Krahn

Alont said:
$body is a html file waiting for process,
my purpose very simple----I want get the content of $body from
"<!--head-->" to "<!--bottom -->"

Looks like it's time to read the Perl regular expressions tutorial.

perldoc perlretut

$body =~ {<!--head-->.*<!--bottom -->};

If you want to delimit the match operator with '{' and '}' then you have to
indicate that you are using the match operator by prepending an 'm' character
or use the standard '/' match delimiters.

$body =~ m{<!--head-->.*<!--bottom -->};

print $body;

The match operator by itself does not modify the string it is bound to. You
probably want to use parentheses to capture the desired text.

the error record:
File::Glob object version 0.991 does not match bootstrap parameter
1.03 at F:/Perl/lib/XSLoader.pm line 95.

BTW: What's the string connect operator in Perl? for example:
there have $head, $body, $trailer and I want make them to one $
variable, if in VB I can worte: $all=$head & $body & trailer, I have
searched perlop - Perl operators and precedence Document, find
nothing about this

TMTOWTDI

my $all = "$head$body$trailer";

my $all = $head . $body . $trailer;

my $all = join '', $head, $body, $trailer;

my $all = pack 'A*A*A*', $head, $body, $trailer;

my $all = sprintf '%s%s%s', $head, $body, $trailer;



John
 
M

Mr. M.J. Lush

this expression always don't work in my Perl compiler,
and compiler don't give me error information,
I've tried this:
$text_start = "<div id="boxtitle">";
$text_end = "<div id="bottom">";
my $content =~ $body =~ /($text_start.*$text_end)/;
this also no result.

Converting to a proper program I get:-

#!/usr/bin/perl -w
use strict;

my $body = '<div id="boxtitle">wibblewibblewibble<div id="bottom">';

my $text_start = "<div id="boxtitle">";
my $text_end = "<div id="bottom">";
my $content =~ $body =~ /($text_start.*$text_end)/;
print $content;

Which produces errors, if it does not, there is something wrong with
your computer!

To kick off you need to single quote the $text_start $text_end
so the quote marks in the string are not interpolated
and remove the ~ between $content and $body

#!/usr/bin/perl -w
use strict;

my $body = '<div id="boxtitle">wibblewibblewibble<div id="bottom">';
my $text_start = '<div id="boxtitle">';
my $text_end = '<div id="bottom">';
my $content = $body =~ /($text_start.*$text_end)/;
print $content;

the program now runs but prints '1' this is because
$body =~ /($text_start.*$text_end)/; returns an list of
matches found in between the brackets.

my $content =

puts the expression in a scalar context, when an list is treated
in a scalar context, perl returns the number of elements in the list
and puts that into the scalar. Since $body =~ /($text_start.*$text_end)/;
can only make a single match $content gets the number 1.

To force perl to look at $content in a list context put brackets round
it. In the same way you can declare an array @array = ("a", "b", "c");

#!/usr/bin/perl -w
use strict;

my $body = '<div id="boxtitle">wibblewibblewibble<div id="bottom">';

my $text_start = '<div id="boxtitle">';
my $text_end = '<div id="bottom">';
my ($content) = $body =~ /($text_start.*$text_end)/;
print $content;

for bounus points if you make the match global and replace $content
with an array.

my @content = $body =~ /($text_start.*$text_end)/g;

5~@content gets _all_ instances of <div id="boxtitle">.*<div id="bottom">
in $body.

Hope this helps.
 
T

Tore Aursand

I've tried this:
$text_start = "<div id="boxtitle">";
$text_end = "<div id="bottom">";
my $content =~ $body =~ /($text_start.*$text_end)/;
this also no result.

Of course, 'cause it doesn't even run. Show us a script that actually
work, and then we'll help you.
 
A

Alont

(e-mail address removed) (Mr. M.J. Lush)Wrote at Sat, 25 Sep 2004 09:59:20
+0000 (UTC):
my $body = '<div id="boxtitle">wibblewibblewibble<div id="bottom">';

my $text_start = '<div id="boxtitle">';
my $text_end = '<div id="bottom">';
my ($content) = $body =~ /($text_start.*$text_end)/;
print $content;

maybe my html file includes some unexpected characters, My Perl
compiler can compile your script correctly but if I instead of $body
to My html file it won't work, how about your opinion?
 
M

Mr. M.J. Lush

(e-mail address removed) (Mr. M.J. Lush)Wrote at Sat, 25 Sep 2004 09:59:20
+0000 (UTC):

maybe my html file includes some unexpected characters, My Perl
compiler can compile your script correctly but if I instead of $body
to My html file it won't work, how about your opinion?

The unexpected character you are looking for is newline (aka \n)
the . in .* matches every character _except_ newline.

try this

my ($content) = $body =~ /($text_start.*$text_end)/s;

The reason why is in <http://www.perldoc.com/perl5.8.0/pod/perlretut.html>
 
A

Alont

(e-mail address removed) (Mr. M.J. Lush)Wrote at Sat, 25 Sep 2004 14:23:28
+0000 (UTC):
The unexpected character you are looking for is newline (aka \n)
the . in .* matches every character _except_ newline.

try this

my ($content) = $body =~ /($text_start.*$text_end)/s;

The reason why is in <http://www.perldoc.com/perl5.8.0/pod/perlretut.html>
--

you are right!y
If haven't your help, I'll waste more time on this problem, really
thank you :)
 
T

Tad McClellan

Alont said:
and compiler don't give me error information,
I've tried this: ^^^^^^^^^^^^^^^
$text_start = "<div id="boxtitle">";


I don't believe you.

Either you did not try that, or you did get some error information...

If you don't give the correct symptoms, we can't give the correct cure.

Please post your real code (use copy/paste, don't re-type it).
 
T

Tad McClellan

Alont said:
(e-mail address removed) (Mr. M.J. Lush)Wrote at Sat, 25 Sep 2004 14:23:28
+0000 (UTC): ^^
^^


If the start/end sequence might occur more than one time, then
you had better also check the answer to this Perl FAQ:

perldoc -q regex

What does it mean that regexes are greedy? How can I get around it?
 
M

Mr. M.J. Lush

Mr. M.J. Lush said:
?my @content = $body =~ /($text_start.*$text_end)/g;
?
?@content gets _all_ instances of <div id="boxtitle">.*<div id="bottom">
?in $body.

Surely:
my @content = $body =~ /($text_start.*?$text_end)/g;

otherwise you get everything between the first start tag and the last end
tag into the first element of the array.

True, but it does do exactly what it says on the tin. All instances of
the patten do get put in the array.....
..
Trys
..
to
..
keep
..
straight
..
face
..
OK its a fair cop guvnor, my bad.
 
A

Alont

Tad McClellan said:
I don't believe you.

Either you did not try that, or you did get some error information...

If you don't give the correct symptoms, we can't give the correct cure.

Please post your real code (use copy/paste, don't re-type it).

I have no motive for cheat people here, not everything you can suspect
on computer programming, what I posted really my real code. I've
solve the problem after the last post Mr. M.J. Lush wrote.
 
J

Joe Smith

Alont said:
I have no motive for cheat people here, not everything you can suspect
on computer programming, what I posted really my real code.

But you posted:
$text_start = "<div id="boxtitle">";
instead of
$text_start = '<div id="boxtitle">';
or
$text_start = q(<div id="boxtitle");

Since the one that you posted (the first one) does not compile, we are
certain that you did not post your real code. Little differences like
single quotes versus double quotes can make a big difference. That's
why we ask that you post (copy-and-paste) your real code.
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top