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:
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 »
January 27th, 2007 at 7:39 pm
That’s not a ‘\’, it’s actually a ‘\fnord’.
January 28th, 2007 at 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
January 28th, 2007 at 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.
March 15th, 2007 at 6:51 am
You can use RegexTester to test a NSPredicated-based regex against a string.
http://www.stiefels.net/projects/regextester
August 18th, 2008 at 6:22 am
thanks for this info
September 3rd, 2008 at 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.
September 3rd, 2008 at 10:02 pm
That last post ate the escaped zero, it should show ” in front of the instead.
June 24th, 2009 at 1:17 pm
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).
August 18th, 2011 at 11:15 pm
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.
September 14th, 2011 at 8:57 am
Beek, you made my day. I’ve been searching forever.
October 24th, 2012 at 5:01 pm
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.