Solved

Seperation of tags from text in HTML file

Posted on 2007-12-04
5
379 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

772 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