Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Seperation of tags from text in HTML file

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
alert(innerHTML); 8 32
JavaScript Regex not working for 1-3 digit number?   /^\d{1,3}$/ 5 26
Filktering Alphabetically 8 28
Html value of radio 14 27
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

792 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