Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Howto: Parse HTML tags to get content in between 3

Status
Not open for further replies.

thenewa2x

Programmer
Dec 10, 2002
349
0
0
US
I want to get whatever is in between these two tags where ever it might be in an HTML file:

<special>
</special>

---------------------------------------
If you helped... thx.
 
Is anyone available for help?

---------------------------------------
If you helped... thx.
 
I am NOT good at regexes, but I'll throw on at you. No guarantee that more than 1% of it will be correct though :)

Code:
/<special>(.*)<\/special>/gi;

I was told the .* is picky, which may be the case but I tried. If any one else in here has a nice place to go for regex explanations, feel free to post a URL. I'd like to take a look.
 
$/ = undef;

open (INFILE, "< special.html");
$html = <INFILE>;
close INFILE;

$html =~ m|<special>(.+)</special>|s;

print "$1\n";


Kind Regards
Duncan
 
thx everyone for your help. I took your sample duncan.

Now what i would like to do is replace whatever is in between <special></special> with something is $new_content.

---------------------------------------
If you helped... thx.
 
thanks for the stars!

$html = '<html>
<head>
blah blah blah
<special>
blah blah blah blah blah blah blah blah blah blah blah blah
</special>
</head>
<body>
blah blah blah blah blah blah
</body>
</html>';

print "before:\n——————\n$html\n\n";

$new_content = 'lots of new stuff here';

$html =~ s|<special>(.+)</special>|<special>\n$new_content\n</special>|s;

print "after:\n—————\n$html\n\n";


Kind Regards
Duncan
 
thx for the help, my script works now.

---------------------------------------
If you helped... thx.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top