Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old August 29th, 2000, 10:08 AM
Alice in Wonderland Alice in Wonderland is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Posts: 5 Alice in Wonderland User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi,
im trying to extract specific tags from a html-file. The tag to be "killed" is passed over as second argument $ARGV[1] to the function.

The following line is used to detect all tags containing $ARGV[1]:

$_=~s/</?($ARGV[1])[^>]*>//gs;

< # matching starts with <
/? # one or none /
($ARGV[1]) # the tag
[^>]* # none, one or some characters but no >
> # and the closing >

and the whole stuff should be replaced by nothing // i.e. simply wiped out.

But, apparently there's a bug (a least one) in it because the existing file is simply duplicated, i.e. the regular expression doesn't replace anything.
I did several tests with evaluating the RegExp first, but this doesn't seem to help in any way...
I think the problem is the syntax for incorporating variables into regular expressions. I thought I had to do $ARGV[1] rather than $ARGV[1] to prevent the $ from being read as "End of String"...but...
Does anybody know any solution to this? Thanks in advance.


Reply With Quote
  #2  
Old August 29th, 2000, 05:03 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
>>the regular expression doesn't replace anything.

If you want to overwrite that file, you then need to put it into array, then open that file, wipe everything out, then print the array back to that file.

Reply With Quote
  #3  
Old August 29th, 2000, 05:24 PM
christucker2 christucker2 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Posts: 81 christucker2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Your regexp is wrong. You escape the $ on the var when you want the variable to be interpolated. Also, when writing stuff with /'s in it, it's best to choose a different delimiter for the regexp -- it makes things a little less cluttered. Try this regexp:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
$_ =~ s!</?$ARGV[1][^>]*>!!gm;
[/code]

You'll also be wanting to ensure that you're slurping the entire file into $_ as a tag could potentially break over multiple lines, and the regexp wouldn't be matching on that. To slurp the file, do something like:

<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
{
local $/;
undef $/;
$_ = <FILEHANDLE>;
}
[/code]

The { and } are important(ish) to make the $/ local only to that code block. That way your old $/ (probably the default of n, unless you've been playing) will be restored.

Oh, also note that I changed the s (single line) switch on your regexp to an m (multiline) as well, as that'll make sure breaks over lines behave nicely. Not that you really need to, but it means if you start sticking in $'s and ^'s at some later date it'll probably behave more how you expect.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Regular Expression & Variables


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway