Regular Expression Search And Replace in Xcode

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
I'm attempting to do some massive search and replaces in Xcode, but it's not working out. My search is #include <\S+> and I want to replace the <> with "". I've tried #include "\1" as the replacement text but then I just get #include "" in my file. I tried doing #include "\0" as the replace but then I just get the whole line that search found inside the quotes. How can I just get the file out of the search to replace the <>'s with ""'s?
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
you cant put any regex in the replace field, use sed instead.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #3
Thanks for the tip. I've written a small script that does this pretty well:

Code:
#!/bin/sh
sed 's_#include <\([a-z,A-Z,\/]*\).h>_#include "\1.h"_' $1 > .temp
rm $1
cp .temp $1
rm .temp

Is there any way to make it so that I can just pass all header files and .cpp files to the script and have it work? It currently doesn't seem to like me trying this:

Code:
~/Desktop > ./ReplaceIncludes *.h
sed: test: No such file or directory
sed: copy: No such file or directory
sed: 1.h.h: No such file or directory
rm: test: No such file or directory
rm: copy: No such file or directory
rm: 1.h.h: No such file or directory

But the script works perfect if I just call ./ReplaceIncludes test.h. Any ideas?
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #4
You can use regular expressions in TextWrangler (free), and do a search & replace on multiple files/folders.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #5
unknown Wrote:you cant put any regex in the replace field, use sed instead.

After playing with sed and reading more about the regular expressions, I can in fact accomplish this in Xcode. For the search I used to just have:

#include <\S+>

and replace with

#include "\1">

but all I got was

#include "".

To fix this I changed the search to using parenthesis like this:

#include <(\S+)>

and then it worked perfectly.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  framework search path scarypajamas 0 3,252 Dec 23, 2008 01:50 PM
Last Post: scarypajamas
  Xcode and library search paths WhatMeWorry 4 7,449 Dec 11, 2006 04:09 PM
Last Post: WhatMeWorry