Solved

Seperation of tags from text in HTML file

Posted on 2007-12-04
5
374 Views
Last Modified: 2010-05-18
Hi,

I'm trying to find a regex (in Ruby) to split up an html document so that tags are on their own lines, seperate from the text. See examples below.

There is a complication in that I'm evaluating the file line by line, but sometimes a tag covers more than one line.
<div align="center"><i>All Rights Reserved</i> </div>
 

<!-- should become: -->
 

<div align="center">

<i>

All Rights Reserved

</i>

</div>
 
 

<!-- the regex I've developed is:

    /(<[^>]*>?)*([^<]*)?/

    but this fails to note some tags after text.

    To exemplify, the above example becomes: -->
 

<div align="center">

<i>

All Rights Reserved

</div>
 

<!-- missing the </i> -->

Open in new window

0
Comment
Question by:Synthetics
  • 3
5 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20402653
I would assume that this regex is OK
/(<[^>]*>?)*([^<]*)?/
but you have to make sure that you use a technique that allows multiple replacements in a single line

do you actually want to remove the line breaks inside the tag?

I would go for this regex
/(<[^<>]+(>?))/

I have an extra pair of (), so you can test whether the start of the tag falls on the end of line
then I would put a newline before the tag opener and a newline before the tag closer, if it is there
No need for matching the text in between tags
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20402663
PS, in order for having this working on all occurences of a tag, use gsub()

cheers

Geert
0
 
LVL 5

Author Comment

by:Synthetics
ID: 20402672
That only seemed to capture the <!DOCTYPE ... > for some reason. My output code is below.
f = File.open("input.htm")

out = File.open("output.htm","w")

f.each do |line|

	tags = line.scan(/(<[^<>]+(>?))/)

	newlines = tags*"\n"

	newlines.each do |newline|

		if newline.chomp!.length > 0 then out.puts(newline) end

	end

end #f.each

out.close

f.close

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20402780
have you tried something along these lines?
f = File.open("an-html-file.htm")
f.each do |line|
      puts line.gsub(/(<[^<>]+(>?))/){|s| puts $1}
end #f.each
f.close
0
 
LVL 5

Accepted Solution

by:
dberner9 earned 500 total points
ID: 20833469
I've just done the following in irb:

line = %(<div align="center"><i>All Rights Reserved</i> </div>)
divided = line.scan(/(<[^>]*>|[^<]*)/).flatten.join("\n")
puts divided

which yields:

<div align="center">
<i>
All Rights Reserved
</i>
 
</div>

Does that work for you?
line.scan(/<[^>]*>|[^<]*/).join("\n")

Open in new window

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction The Google Maps API offers various ways to draw dynamic and static maps.  Using a combination of PHP and JavaScript, you can draw active JavaScript maps that allow pan-and-zoom in the client browser window.  You can also draw "static" …
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now