473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

regex puzzle!

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?
Jul 21 '05 #1
8 1452
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?

Jul 21 '05 #2
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" <ga**********@yahoo.com> wrote in
news:25*************************@posting.google.co 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?

Jul 21 '05 #3
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 <An****@discussions.microsoft.com> wrote in message news:<72**********************************@microso ft.com>...
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?

Jul 21 '05 #4
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" <ni*********@cube.net> wrote in message news:<OC**************@TK2MSFTNGP12.phx.gbl>...
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" <ga**********@yahoo.com> wrote in
news:25*************************@posting.google.co 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?

Jul 21 '05 #5
"G. Stewart" <ga**********@yahoo.com> wrote in
news:25**************************@posting.google.c 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
Jul 21 '05 #6
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" <ni*********@cube.net> wrote in message news:<eN**************@TK2MSFTNGP09.phx.gbl>...
"G. Stewart" <ga**********@yahoo.com> wrote in
news:25**************************@posting.google.c 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

Jul 21 '05 #7
"G. Stewart" <ga**********@yahoo.com> wrote in
news:25**************************@posting.google.c 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
Jul 21 '05 #8
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" <ni*********@cube.net> wrote in message news:<eN**************@TK2MSFTNGP09.phx.gbl>...
"G. Stewart" <ga**********@yahoo.com> wrote in
news:25**************************@posting.google.c 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

Jul 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alan Pretre | last post by:
Can anyone help me figure out a regex pattern for the following input example: xxx:a=b,c=d,yyy:e=f,zzz:www:g=h,i=j,l=m I would want four matches from this: 1. xxx a=b,c=d 2. yyy e=f 3....
6
by: Alan Pretre | last post by:
Can anyone help me figure out a regex pattern for the following input example: xxx:a=b,c=d,yyy:e=f,zzz:www:g=h,i=j,l=m I would want four matches from this: 1. xxx a=b,c=d 2. yyy e=f 3....
8
by: G. Stewart | last post by:
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...
3
by: Alan Pretre | last post by:
Can anyone help me figure out a regex pattern for the following input example: xxx:a=b,c=d,yyy:e=f,zzz:www:g=h,i=j,l=m I would want four matches from this: 1. xxx a=b,c=d 2. yyy e=f 3....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.