Search and Replace in Xcode

This issue came up twice in the past couple weeks thus triggering my rule that if something comes up more than once then others may be interested in it. So, here’s a little tip that may save you some frustration.

When using regular expressions with Xcode’s search, the docs mention that XCode uses the ICU library. Naturally, you’d think it would use ICU’s syntax for specifying backreferences in the replace string, which would be to use variables consisting of a dollar sign ($) followed by the number of the capture group being referenced.

Of course, if that were the case, I probably wouldn’t be writing this tip. Using $ for backreferences turns “Search & Replace” into “Search & Destroy”. The syntax is to use backslash (\) instead of dollar sign. In short, \1 instead of $1.

Type the following in an Xcode editor window: “$1 works in replace strings.”

Now perform the following search & replace:

find-replace-crop.png

Ok, maybe this example makes things more confusing. If in doubt, just remember: $ = bad, \ = good in Xcode Find panel.

As to why Xcode does not use the ICU syntax in the replace strings, beats me. If anyone has a simple explanation, send it my way though I also welcome apocryphal anecdotes and crackpot conspiracy theories (extra points if you can convincingly implicate the Trilateral Commission).

7 Comments »

  1. Jon H Said,

    January 27, 2007 @ 7:39 pm

    That’s not a ‘\’, it’s actually a ‘\fnord’.

  2. Aaron Evans Said,

    January 28, 2007 @ 9:44 am

    Although I don’t know Apple’s direct intentions for using ICU, ICU was most likely chosen because of its wide acceptance as a very high quality, Unicode compliant regular expression library. My suspicion is that Apple (internally) uses ICU for many tasks other than regular expression matching. ICU is packaed as a binary on OS X but no headers are included.

    I am the author of an Objective-C wrapper for the ICU regular expression library:
    http://aarone.org/cocoaicu

  3. mr_noodle Said,

    January 28, 2007 @ 12:49 pm

    Jon: That explains the general paranoia I feel whenever doing search and replace. Thanks.

    Aaron: My question was not why ICU so much as why not ICU in both the search and replace strings. If they use ICU syntax for the match pattern, you’d think they also use it for the replace pattern. Thanks for the link. Uwe Hoffman also did a similar thing: http://www.codemanic.com/blog-archives/000088.html

    Also, for those who only need to do straight regex matching (no capture groups or replacement), NSPredicate (which is also backed by ICU) can be used.

  4. jerome Said,

    March 15, 2007 @ 6:51 am

    You can use RegexTester to test a NSPredicated-based regex against a string.
    http://www.stiefels.net/projects/regextester

  5. Geoffrey Said,

    August 18, 2008 @ 6:22 am

    thanks for this info

  6. Shannon Said,

    September 3, 2008 @ 10:01 pm

    This was helpful, but it took me some time to realize that you start counting at zero, not one, so to backtrack / use the text you found, you need to use the instead. Thanks for the help.

  7. Shannon Said,

    September 3, 2008 @ 10:02 pm

    That last post ate the escaped zero, it should show ” in front of the instead.

RSS feed for comments on this post

Leave a Comment

- Why ask? This confirms you are a human user!