Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > regex puzzle!

Reply
Thread Tools

regex puzzle!

 
 
G. Stewart
Guest
Posts: n/a
 
      11-23-2004
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?
 
Reply With Quote
 
 
 
 
=?Utf-8?B?QW5kcmV3?=
Guest
Posts: n/a
 
      11-23-2004
Regex's don't count. You may need to look into some grammar tools to
accomplish this. Or write some custom code.

"G. Stewart" wrote:

> 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?
>

 
Reply With Quote
 
 
 
 
Niki Estner
Guest
Posts: n/a
 
      11-24-2004
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" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) m...
> 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?



 
Reply With Quote
 
G. Stewart
Guest
Posts: n/a
 
      11-24-2004
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 <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Regex's don't count. You may need to look into some grammar tools to
> accomplish this. Or write some custom code.
>
> "G. Stewart" wrote:
>
> > 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?
> >

 
Reply With Quote
 
G. Stewart
Guest
Posts: n/a
 
      11-24-2004
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" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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" <(E-Mail Removed)> wrote in
> news:(E-Mail Removed) m...
> > 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?

 
Reply With Quote
 
Niki Estner
Guest
Posts: n/a
 
      11-24-2004
"G. Stewart" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
G. Stewart
Guest
Posts: n/a
 
      11-24-2004
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.



"Niki Estner" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> "G. Stewart" <(E-Mail Removed)> wrote in
> news:(E-Mail Removed) om...
> > 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

 
Reply With Quote
 
Niki Estner
Guest
Posts: n/a
 
      11-25-2004
"G. Stewart" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
G. Stewart
Guest
Posts: n/a
 
      11-25-2004
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

"Niki Estner" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> "G. Stewart" <(E-Mail Removed)> wrote in
> news:(E-Mail Removed) om...
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments