regex puzzle!

Discussion in 'ASP .Net' started by G. Stewart, Nov 23, 2004.

  1. G. Stewart

    G. Stewart Guest

    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, Nov 23, 2004
    #1
    1. Advertising

  2. 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?
    >
    =?Utf-8?B?QW5kcmV3?=, Nov 23, 2004
    #2
    1. Advertising

  3. G. Stewart

    Niki Estner Guest

    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" <> wrote in
    news:...
    > 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 Estner, Nov 24, 2004
    #3
  4. G. Stewart

    G. Stewart Guest

    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 <> wrote in message news:<>...
    > 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?
    > >
    G. Stewart, Nov 24, 2004
    #4
  5. G. Stewart

    G. Stewart Guest

    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" <> wrote in message news:<>...
    > 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" <> wrote in
    > news:...
    > > 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, Nov 24, 2004
    #5
  6. G. Stewart

    Niki Estner Guest

    "G. Stewart" <> wrote in
    news:...
    > 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 Estner, Nov 24, 2004
    #6
  7. G. Stewart

    G. Stewart Guest

    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" <> wrote in message news:<>...
    > "G. Stewart" <> wrote in
    > news:...
    > > 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
    G. Stewart, Nov 24, 2004
    #7
  8. G. Stewart

    Niki Estner Guest

    "G. Stewart" <> wrote in
    news:...
    > 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 Estner, Nov 25, 2004
    #8
  9. G. Stewart

    G. Stewart Guest

    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" <> wrote in message news:<>...
    > "G. Stewart" <> wrote in
    > news:...
    > > 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
    G. Stewart, Nov 25, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    690
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Jeremy Bowers

    a RegEx puzzle

    Jeremy Bowers, Mar 11, 2005, in forum: Python
    Replies:
    4
    Views:
    357
    Kent Johnson
    Mar 11, 2005
  3. Replies:
    3
    Views:
    734
    Reedick, Andrew
    Jul 1, 2008
  4. Roedy Green

    Regex Puzzle

    Roedy Green, Dec 14, 2008, in forum: Java
    Replies:
    5
    Views:
    506
  5. Roedy Green

    regex puzzle

    Roedy Green, Apr 9, 2014, in forum: Java
    Replies:
    8
    Views:
    103
    Arne Vajhøj
    Apr 12, 2014
Loading...

Share This Page