7 Replies Latest reply on Aug 7, 2008 1:53 PM by Newsgroup_User

    RegEx help

    Balance Level 1
      I need to convert this:

      <a href=" http://www.cnn.com"><img src=" http://some.domain.net/images/test1.gif" width="143" height="107" hspace="3" vspace="3" border="0" id="Content1"></a>

      to this:
      *Content1*

      Basically, use the value of the ID attribute and encapsulate with asterisks.
      The kicker is that the href and img src can be anything, the values posted here are just placeholders.

      TIA
        • 1. Re: RegEx help
          Level 7
          On Thu, 7 Aug 2008 15:00:13 +0000 (UTC), Balance wrote:

          > I need to convert this:
          >
          > <a href=" http://www.cnn.com"><img
          > src=" http://some.domain.net/images/test1.gif" width="143" height="107"
          > hspace="3" vspace="3" border="0" id="Content1"></a>
          >
          > to this:
          > *Content1*
          >
          > Basically, use the value of the ID attribute and encapsulate with asterisks.
          > The kicker is that the href and img src can be anything, the values posted
          > here are just placeholders.

          Is it the "the value of the ID attribute and encapsulate with asterisks" or
          "the value of the ID attribute OF THE <img> tag which is within a <a> tag,
          and encapsulate with asterisks"?

          Because those are two different things.

          NB: a work in progress for the latter is:
          ^<a[^>]+?><img[^>]*?id="([^"]+?)"[^>]*?></a>$

          That makes some assumptions about your requirements.

          --
          Adam
          • 2. Re: RegEx help
            Balance Level 1
            Adam,

            Fair enough, the value of the ID attribute of the <img> tag which is within a <a> tag needs to be wrapped with asterisks. I've tried your sample code but nothing gets replaced; the full HTML code comes back. Here's my code:

            <cfsavecontent variable="strText">
            <a href=" http://www.cnn.com">
            <img src=" http://some.domain.net/images/test1.gif" width="143" height="107" hspace="3" vspace="3" border="0" id="HTMLContent1">
            </a>
            </cfsavecontent>

            <cfoutput>
            #REReplaceNoCase(strText,'^<a[^>]+?><img[^>]*?id="([^"]+?)"[^>]*?></a>$',"","ALL")#
            <br /><br />
            <xmp>#REReplaceNoCase(strText,'^<a[^>]+?><img[^>]*?id="([^"]+?)"[^>]*?></a>$',"","ALL")#</ xmp>
            </cfoutput>
            • 3. Re: RegEx help
              Stressed_Simon Level 1
              Try this
              • 4. Re: RegEx help
                Balance Level 1
                Simon - This seems to work like a charm. I will try a few other variations and mark your post as the answer if testing looks good. THANK YOU!!
                • 5. Re: RegEx help
                  Stressed_Simon Level 1
                  No worries, Adam's code was correct for what you originally posted. It didn't cater for the line breaks in your code that is all.
                  • 6. RegEx help
                    Balance Level 1
                    Ugh. I just found that the ID attribute of the img src may or may not be wrapped in double quotes. So, it's possible for it to look like this:

                    <img src=" http://some.domain.net/images/test1.gif" width="143" height="107" hspace="3" vspace="3" border="0" id="HTMLContent1">

                    OR

                    <IMG id=*HTMLContent1* height=107 hspace=3 src=" http://some.domain.net/images/test1.gif" width=143 vspace=3 border=0>

                    The reg ex works with the first scenario but not the second. Also, please notice how the order in which the ID attribute is coded in the second case is different from the first case. Not sure if this makes a difference. Can someone shed some light?

                    Thanks again
                    • 7. Re: RegEx help
                      Level 7
                      Rather than just expecting the answers - which will teach you nowt - why
                      doing you read up on regexes and try to DIY.

                      You could at least have a crack at it, come back to us with your attempt.

                      --
                      Adam