Skip to main content
Inspiring
August 7, 2008
Question

RegEx help

  • August 7, 2008
  • 5 replies
  • 661 views
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
This topic has been closed for replies.

5 replies

Inspiring
August 7, 2008
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
BalanceAuthor
Inspiring
August 7, 2008
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
BalanceAuthor
Inspiring
August 7, 2008
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!!
Inspiring
August 7, 2008
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.
BalanceAuthor
Inspiring
August 7, 2008
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>
Inspiring
August 7, 2008
Try this
Inspiring
August 7, 2008
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