• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

RegEx help

Explorer ,
Aug 07, 2008 Aug 07, 2008

Copy link to clipboard

Copied

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
TOPICS
Advanced techniques

Views

481

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 07, 2008 Aug 07, 2008

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 07, 2008 Aug 07, 2008

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 07, 2008 Aug 07, 2008

Copy link to clipboard

Copied

Try this

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 07, 2008 Aug 07, 2008

Copy link to clipboard

Copied

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!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 07, 2008 Aug 07, 2008

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 07, 2008 Aug 07, 2008

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 07, 2008 Aug 07, 2008

Copy link to clipboard

Copied

LATEST
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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation