Solved

Regular expression to replace tags

Posted on 2004-10-20
6
610 Views
Last Modified: 2013-12-24
Does anyone have a regular expression that will remove any tag and the content between the tags and replace it with the blank tag value? For instance if I want to remove the "<head></head>" tags and any character and/or nested tags between them and replace them with "<head><title>My Title</title></head>".
0
Comment
Question by:McHack
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 35

Expert Comment

by:mrichmon
ID: 12362885
You could do it in two steps.

Step 1) Replace <head>....</head> with <head></head>

You could use a regular expression similar to:
<head>*</head>

Step 2) Then you can replace <head></head> with what you want using a simple replace.


You could probably combine into one step, up to you.
0
 

Author Comment

by:McHack
ID: 12363580
Ok, that didn't work. When I ran that example the page "<HEAD></HEAD>" tags were not removed and the content in between them was not removed of which there is a ton (javascripts, meta tags etc.).

Here is the code I was running:

<CFTRY>
       <cfhttp method="get" url="http://www.someurl.com" resolveurl="yes">
        </cfhttp>
      <CFCATCH>
            <CFSET ThrowError = true>
      </CFCATCH>
</CFTRY>
<cfset ApScriptEdit = #ReReplaceNoCase(cfhttp.FileContent, "<head>*</head>", "", "ALL")#>
<Cfoutput>#ApScriptEdit#</Cfoutput>
0
 
LVL 10

Expert Comment

by:Mause
ID: 12366895
Hi

Almost there try this:

<cfset ApScriptEdit  = rereplacenocase(cfhttp.FileContent, "<head>.*?</head>","","ALL")>

Mause
0
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 
LVL 5

Accepted Solution

by:
umbrae earned 500 total points
ID: 12374351
<CFTRY>
       <cfhttp method="get" url="http://www.yahoo.com" resolveurl="yes">
        </cfhttp>
      <CFCATCH>
            <CFSET ThrowError = true>
      </CFCATCH>
</CFTRY>
<cfset ApScriptEdit =
 reReplaceNoCase(cfhttp.fileContent,
"<head[^>]*>.*</head[^>]*>","<head><title>My Title</title></head>","ALL")>
<Cfoutput>#HTMLCodeFormat(ApScriptEdit)#</Cfoutput>

in order to catch all of the possibilities I'd probably using something like this. It'd catch if they have anything inside the head (standard html does not, but they may have a space or something like that).

Just my 2 cents.
0
 
LVL 5

Expert Comment

by:umbrae
ID: 12374358
Whoops. Probably want to remove the htmlCodeFormat() from that output, was doing that for debugging.

-Umbrae
0
 

Author Comment

by:McHack
ID: 12376494
umbrae

Works great, just what I was looking for.

It looks so simple when I see the solution but some how I never seem to get the regular expressions right.

Thanks for the help!

McHack
0

Featured Post

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Two nodes for updates and forwarding 8 51
CFFILE upload help 98 207
exchange 2010 turning off 3des ciphers 2 557
Standalone trial or freeware to do SSL scan 4 81
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question