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).

Category: OS X, Programming, Software, Xcode 11 comments »

11 Responses to “Search and Replace in Xcode”

  1. Jon H

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

  2. Aaron Evans

    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

    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

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

  5. Geoffrey

    thanks for this info

  6. Shannon

    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

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

  8. Paul Lustgarten

    More than two years later, now on Xcode version 3.1.3, and this is still helpful info.

    As for the numbering of the capture groups as replacements (of the form backslash-digits), it appears that 0 (zero) yields the *entire* matched string (which seems to be implicitly defined as a capture group), whilst 1 (one) yields the first capture group that is explicitly defined in the match string.

    Now, why this isn’t in the Xcode documentation anywhere, even after all these years, is quite a mystery (right next to the original question of why Apple used a different replacement notation than the ICU package uses).

  9. Beek

    Still holds true for Xcode 4.x. The screen is now a bit different, though. If you want to use Regular Expressions in Xcode 4, you must first hit the dropdown button next to the magnifying glass in the Find box, select Show Find Options, then select Regular Expressions from the Style dropdown.

  10. Paul

    Beek, you made my day. I’ve been searching forever.

  11. Alex Gray

    Steve just HATED those FOOLS over at ICU. can you say “tacky”?

    However… Tim Cooke says you can use whatever syntax you want… as long as you “love him”… whatever that means.


Leave a Reply



Back to top