regex puzzle!

  • Thread starter Thread starter G. Stewart
  • Start date Start date
G

G. Stewart

The objective is to extract the first n characters of text from an
HTML block. I wish to preserve all HTML (links, formatting etc.), and
at the same time, extend the size of the block to ensure that all
closing tags are recovered.

For example, simply extracting the first 400 characters of a HTML
block may result in an <i> opening tag being including, but its
closing tag being excluding. Or a link may get chopped halfway - [...
blah blah <a href="ht] may be the last few characters of the recovered
phrase.

Ideally, if any html opening tag is included in the first n
characters, then any number of extra characters should continue to be
extracted from the source block until all paired closing tags are
found.

We can assume that the source block is well-formed HTML, and every
opening tag has a closing tag (whether optional or not). Furthermore
(if it makes any difference), we can assume that all tags are given in
their simplest forms with no attributes (e.g. <p>, <ul>, <li>, <b>),
except for anchor tags, which have the href attribute of course.

Can anyone suggest a regular expression to do this?
 
Regex's don't count. You may need to look into some grammar tools to
accomplish this. Or write some custom code.
 
A regex like this one:
^((<[^>]*>)*[^<]){400}
will extract 400 characters from an HTML source, not counting any HTML-tags
(i.e. ignoring characters between <...>), but I'm not sure about the
opening/closing-tag matching: I think it is possible to do this (thanks to
certain specials in MS' regex implementation), however, as a usual HTML
pages start with a "body" tag, that spans the entire page I'm not sure if
this is really what you want.

Niki
 
Hmmm ... Allright. I was hoping for something quick and efficient, but
it look like I might have to do things the hard way - extract the
n-character block, count opening tags, count closing tags, then
continue extracting characters from the source block until all opening
tags have paired closing tags.

Andrew said:
Regex's don't count. You may need to look into some grammar tools to
accomplish this. Or write some custom code.

G. Stewart said:
The objective is to extract the first n characters of text from an
HTML block. I wish to preserve all HTML (links, formatting etc.), and
at the same time, extend the size of the block to ensure that all
closing tags are recovered.

For example, simply extracting the first 400 characters of a HTML
block may result in an <i> opening tag being including, but its
closing tag being excluding. Or a link may get chopped halfway - [...
blah blah <a href="ht] may be the last few characters of the recovered
phrase.

Ideally, if any html opening tag is included in the first n
characters, then any number of extra characters should continue to be
extracted from the source block until all paired closing tags are
found.

We can assume that the source block is well-formed HTML, and every
opening tag has a closing tag (whether optional or not). Furthermore
(if it makes any difference), we can assume that all tags are given in
their simplest forms with no attributes (e.g. <p>, <ul>, <li>, <b>),
except for anchor tags, which have the href attribute of course.

Can anyone suggest a regular expression to do this?
 
Niki:

Thanks. The HTML source that I am extracting from does not include
<html>, <head>, or <body> tags. Just the block or in-line element
tags: <p>, <i>, <em>, <a>, etc.

What I want to do is to extract a snippet or preview of the source
block, while preserving all the html tags in the snippet/preview,
including formatting and links. Any ideas?


Niki Estner said:
A regex like this one:
^((<[^>]*>)*[^<]){400}
will extract 400 characters from an HTML source, not counting any HTML-tags
(i.e. ignoring characters between <...>), but I'm not sure about the
opening/closing-tag matching: I think it is possible to do this (thanks to
certain specials in MS' regex implementation), however, as a usual HTML
pages start with a "body" tag, that spans the entire page I'm not sure if
this is really what you want.

Niki

G. Stewart said:
The objective is to extract the first n characters of text from an
HTML block. I wish to preserve all HTML (links, formatting etc.), and
at the same time, extend the size of the block to ensure that all
closing tags are recovered.

For example, simply extracting the first 400 characters of a HTML
block may result in an <i> opening tag being including, but its
closing tag being excluding. Or a link may get chopped halfway - [...
blah blah <a href="ht] may be the last few characters of the recovered
phrase.

Ideally, if any html opening tag is included in the first n
characters, then any number of extra characters should continue to be
extracted from the source block until all paired closing tags are
found.

We can assume that the source block is well-formed HTML, and every
opening tag has a closing tag (whether optional or not). Furthermore
(if it makes any difference), we can assume that all tags are given in
their simplest forms with no attributes (e.g. <p>, <ul>, <li>, <b>),
except for anchor tags, which have the href attribute of course.

Can anyone suggest a regular expression to do this?
 
G. Stewart said:
Niki:

Thanks. The HTML source that I am extracting from does not include
<html>, <head>, or <body> tags. Just the block or in-line element
tags: <p>, <i>, <em>, <a>, etc.

What I want to do is to extract a snippet or preview of the source
block, while preserving all the html tags in the snippet/preview,
including formatting and links. Any ideas?

The point is that matching paranthesis is possible with regex's, but it's
quite tricky (i.e.: I'd have to look it up in a book myself...). However, I
still don't see why you need that; Consider an input like this:
"This text contains <i>italic</i>,<em>bold</em> and even <a
....>hyperlinked</a> text"
If you extract 20 characters from it, not counting tag-characters (using a
regex like the one I've suggested in my previous post) you'd get:
"This text contains <i>i"
Now, if you'd put this in an HTML element like:
"<span>This text contains <i>i</span>..."
So you'd produce correct HTML (not XML). I think this should work for any
input, since the closing-tag's for <p>, <i>, <em>... are all optional.

Niki
 
Niki:

The problem really is with links:

<p>A simple sentence with a <a href="http://dummylink.com/">link</a>
and other stuff</p>.

If the extraction recovers open quote of the opening a tag, but no the
closing quote, then most of the remaining page gets really messed up.
 
G. Stewart said:
Niki:

The problem really is with links:

<p>A simple sentence with a <a href="http://dummylink.com/">link</a>
and other stuff</p>.

If the extraction recovers open quote of the opening a tag, but no the
closing quote, then most of the remaining page gets really messed up.

I just entered that into expresso, using the regex I posted earlier.
Extracting 25 characters yields:
"<p>A simple sentence with a "
(that's 25 characters not counting the <p> tag)
Extracting 26 characters yields:
"<p>A simple sentence with a <a href="http://dummylink.com/">l"

I'm not 100% sure, but I do think most browsers would cope with this, if
it's enclosed within dome other tag (like div, span, table...)

You could modify the regex so it doesn't break the link apart, however then
it would extract more characters than you wanted (and it would get a lot
more complex).

Another alternative is to extract the number of characters as suggested
above, and after that count occurences of "<a" and "</a" - if they don't
match, add "</a>" to the end of the string.

Hope this helps,

Niki
 
Niki:

OK. *NOW* I see what you mean! By ignoring anything within <>, then
the href attribute in links are skipped completely, and, if the entire
results are wrapped in <span></span> tags (to protected against
unclosed <i>, <em> etc.), then everything works out great!

Thanks!

-- jet
 
Back
Top