Solved

Regular expression to replace tags

Posted on 2004-10-20
6
605 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
6 Comments
 
LVL 35

Expert Comment

by:mrichmon
Comment Utility
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
Comment Utility
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
Comment Utility
Hi

Almost there try this:

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

Mause
0
Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

 
LVL 5

Accepted Solution

by:
umbrae earned 500 total points
Comment Utility
<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
Comment Utility
Whoops. Probably want to remove the htmlCodeFormat() from that output, was doing that for debugging.

-Umbrae
0
 

Author Comment

by:McHack
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now