Solved

Seperation of tags from text in HTML file

Posted on 2007-12-04
5
377 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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

911 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

22 Experts available now in Live!

Get 1:1 Help Now