CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Pattern Matching Problem

 



steve
Deleted

Mar 1, 2000, 6:10 PM

Post #1 of 3 (786 views)
Pattern Matching Problem Can't Post

Hello,

I have a problem I can't figure out. What I am trying to do is parse a text file (HTML) that looks something like this.
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


I haven't been able to get this line.
I haven't been able to get this line.
My successful part. <BREAKPOINT> And more text.
And more text. You get the idea.
</pre><HR></BLOCKQUOTE>

My goal is to load everything up to the "<BREAKPOINT>" into one variable, and everthing after into another.

Here is what I came up with but it doesn't work.
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open(TMPL, "admin.tmpl.html")
@tmpl = <TMPL>;
close(TMPL);
$tmpl = join('', @tmpl);
if ($tmpl =~ /(\w*|\n*)<BREAKPOINT>([\w*|\s*])/g) {
$begin = $1;
$end = $2;
}
</pre><HR></BLOCKQUOTE>
I just can't figure out the pattern matching. The best I've been able to do is have $begin contain everything on the SAME line but before "<BREAKPOINT>".

Any pointers in the right direction would be appreciated. I ordered The Pearl Cookbook from Amazon, hopefully that will help me more than this Pearl For Dummies book.

Thanks,
Steve


japhy
Enthusiast

Mar 1, 2000, 7:17 PM

Post #2 of 3 (786 views)
Re: Pattern Matching Problem [In reply to] Can't Post

You can be tricky and use the $/ variable ($INPUT_RECORD_SEPARATOR):

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open FILE, "file" or die "can't open file: $!";
{
local $/ = "<BREAKPOINT>";
chomp($before = <FILE> );
$after = do { local $/, <FILE> };
}
close FILE;
</pre><HR></BLOCKQUOTE>

Now $before and $after hold what you want. Read the 'perlvar' documentation to learn how $/ works.


steve
Deleted

Mar 2, 2000, 5:04 AM

Post #3 of 3 (786 views)
Re: Pattern Matching Problem [In reply to] Can't Post

Thanks! I probably would have never found that on my own.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives