regex puzzle!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • G. Stewart

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

    #2
    RE: regex puzzle!

    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:
    [color=blue]
    > 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?
    >[/color]

    Comment

    • Niki Estner

      #3
      Re: regex puzzle!

      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" <galenstewart@y ahoo.com> wrote in
      news:258fa3a8.0 411222044.6c3b9 ec@posting.goog le.com...[color=blue]
      > 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?[/color]


      Comment

      • G. Stewart

        #4
        Re: regex puzzle!

        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 <Andrew@discuss ions.microsoft. com> wrote in message news:<72F30C38-7F6A-4A53-B056-5AB8A8A36973@mi crosoft.com>...[color=blue]
        > 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:
        >[color=green]
        > > 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?
        > >[/color][/color]

        Comment

        • G. Stewart

          #5
          Re: regex puzzle!

          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" <niki.estner@cu be.net> wrote in message news:<OCz2Aqb0E HA.1264@TK2MSFT NGP12.phx.gbl>. ..[color=blue]
          > 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" <galenstewart@y ahoo.com> wrote in
          > news:258fa3a8.0 411222044.6c3b9 ec@posting.goog le.com...[color=green]
          > > 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?[/color][/color]

          Comment

          • Niki Estner

            #6
            Re: regex puzzle!

            "G. Stewart" <galenstewart@y ahoo.com> wrote in
            news:258fa3a8.0 411240026.67060 5c5@posting.goo gle.com...[color=blue]
            > 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?[/color]

            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
            ....>hyperlinke d</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


            Comment

            • G. Stewart

              #7
              Re: regex puzzle!

              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" <niki.estner@cu be.net> wrote in message news:<eN6nkQh0E HA.1932@TK2MSFT NGP09.phx.gbl>. ..[color=blue]
              > "G. Stewart" <galenstewart@y ahoo.com> wrote in
              > news:258fa3a8.0 411240026.67060 5c5@posting.goo gle.com...[color=green]
              > > 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?[/color]
              >
              > 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[/color]

              Comment

              • Niki Estner

                #8
                Re: regex puzzle!

                "G. Stewart" <galenstewart@y ahoo.com> wrote in
                news:258fa3a8.0 411241458.4329d 870@posting.goo gle.com...[color=blue]
                > 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.[/color]

                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


                Comment

                • G. Stewart

                  #9
                  Re: regex puzzle!

                  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" <niki.estner@cu be.net> wrote in message news:<eN6nkQh0E HA.1932@TK2MSFT NGP09.phx.gbl>. ..[color=blue]
                  > "G. Stewart" <galenstewart@y ahoo.com> wrote in
                  > news:258fa3a8.0 411240026.67060 5c5@posting.goo gle.com...[color=green]
                  > > 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?[/color]
                  >
                  > 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[/color]

                  Comment

                  Working...