Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

memco

macrumors 6502
Original poster
May 1, 2008
261
21
I have an existing site in which I need to replace a lot of code across multiple files quickly. The code is similar, but not identical on all pages, but there are two pieces of code on every page I need: the title and the main body of content. I thought regex was the way to go, but am curious if there's software to handle it.

If not, my regexp currently reads:
Code:
(?:.*?)<(title*)\b[^>]*>(.*?)</\1>(?:.*?)"body"\s*-->(.*?)<--InstanceEndEditable -->(?:.*)

My replacement would be something like:
Code:
<%
pageTitle = "$1"
%>

<!-- Body -->
$2
 
Can post some example code that you'll be applying to regex to? As far as doing a replace across multiple files BBEdit (and it's free version TextWrangler) can do regex find and replace and you can do it on multiple files, though I've never tried it out. You could alternatively write a Perl or PHP script to work its way through all of the files and do the replacing if you have the know how with those languages.

Also, I have a regular expression testing page that I developed that could possibly help you work out the regular expression.
 
I've attached two samples of what is being passed through. The services.asp page is the more common format, and it is more critical that pages like that one make it through the process properly.

I appreciate the suggestions, and the help.
 

Attachments

  • catalog.txt
    653 bytes · Views: 109
  • services.txt
    27.1 KB · Views: 142
Well now, not the easiest of regular expressions. I don't care much for regular expressions that go across multiple lines. I think I have something though.

The RegEx:
Code:
<title>(.*)</title>[^]*.*<body[^<]*>([^]*.*)</body>
My regular expression page I linked to before doesn't quite show the real outcome of this (Edit: it has been fixed now to better handle regex like above), but it seems to capture what you want, if I understand it correctly. With the above, $1 will capture the text inside the title tag, and $2 will capture everything inside the body tag. This is not tested very thoroughly.
 
Thank you very much angelwatt, I've got it working the way I want to. I had to modify what you had just a bit, but nevertheless, it works as advertised.

Code:
[^]*.*?<(title*)\b[^>]*>(.*?)</\1>[^]*.*?"body"\s-->([^]*.*?)<!--\sInstanceEndEditable\s-->(?:.*?)[^]*.*?
 
Thank you very much angelwatt, I've got it working the way I want to. I had to modify what you had just a bit, but nevertheless, it works as advertised.

Code:
[^]*.*?<(title*)\b[^>]*>(.*?)</\1>[^]*.*?"body"\s-->([^]*.*?)<!--\sInstanceEndEditable\s-->(?:.*?)[^]*.*?

Cool, glad I got you on the right track. I've since updated my regular expression tool to better handle things (for those you have visited recently, you'll need to do a hard refresh so it loads the newer javascript). Out of curiosity, where did you use the regex to do the multi-file replace? Was it in a program like BBEdit or did you use it in a PHP or Perl script? I'm always amazed at the power of regular expressions. It was why I created the tool, so I could better understand them.
 
I used an application called Smultron, a very nice and simple editor, to do the replacing. I did download textwrangler and an application called textreme, both of which show promise for regexes, but I didn't use them. Incidentally, working with multiple files is slow. It took me about 30 minutes to an hour to process all the files (less than 50).

That page of yours is pretty handy, thanks for the link.
 
I use to use Smultron, nice app, though didn't remember it having a regex find and replace. It's been a few years since I've used it though so I'm sure they've added features. I knew working on multiple files would take a while, but 30+ minutes is a lot, though that regex is fairly complex. Perl probably could have done it quicker, but you would then have to take extra time to write the script.
 
The regexp is done via advanced find and replace. I don't know how long it's been there, but it's nice to have. Part of the reason it took so long was because I had to do it in batches. Smultron chokes on opening so many files. PERL probably would've been the way to go, but I didn't want to mess with the scripts. Though writing a PHP script for further edits might be a project to work on. If I ever have the time to work on Cocoa apps, building something to handle this would be fun.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.