473,397 Members | 1,969 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,397 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?
Nov 16 '05 #1
8 1210
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?

Nov 16 '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?

Nov 16 '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?

Nov 16 '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?

Nov 16 '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
Nov 16 '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

Nov 16 '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
Nov 16 '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

Nov 16 '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....
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...
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
1
by: Tom | last post by:
A puzzle for you regular expression wizards out there. Looking for a regex that will split up a string like the following on any pipe (|) not inside brackets: a b | a { b |{c | cd}} d | a b c...
4
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.