{"id":80,"date":"2012-02-10T14:27:34","date_gmt":"2012-02-10T11:27:34","guid":{"rendered":"http:\/\/www.cocoaminers.com\/?p=80"},"modified":"2015-04-06T20:19:48","modified_gmt":"2015-04-06T18:19:48","slug":"xcode-search-and-replace-using-regular-expressions-back-references","status":"publish","type":"post","link":"https:\/\/www.cocoaminers.com\/?p=80","title":{"rendered":"Xcode search and replace using regular expressions back references"},"content":{"rendered":"<p>Sometimes you face a little more advanced search&amp;replace need in Xcode. I had to solve this puzzle &#8211; original string, can be placed anywhere in multiple source files:<\/p>\n<p><code>SHKLocalizedString(@\"&lt;<em>whatever<\/em>&gt;\")<\/code><\/p>\n<p>where &lt;whatever&gt; can be any text. Needed\u00a0to change to:<\/p>\n<p><code>SHKLocalizedString(@\"&lt;<em>whatever<\/em>&gt;\"<span style=\"color: #ff0000;\">, nil<\/span>)<\/code><\/p>\n<p>To find the strings using regular expressions enter<\/p>\n<p><code>SHKLocalizedString\\(\\@\".[^\\)]*<\/code><\/p>\n<p>to the search field. OK, but how to tell Xcode to use the result in\u00a0the replace field? Regular expressions offer some help: <span style=\"color: #000000;\"><strong>back references<\/strong><\/span>\u00a0and <strong><span style=\"color: #000000;\">capture groups.<\/span><\/strong>\u00a0First you need to create capture group (think of it as a variable), so let&#8221;s slightly change the search regex:<\/p>\n<p><span style=\"font-family: monospace;\">SHKLocalizedString\\(\\@&#8221;<span style=\"color: #ff0000;\">(<\/span>.[^\\)]*<span style=\"color: #ff0000;\">)<\/span><br \/>\n<\/span><\/p>\n<p>The red parentheses create the group #1 for back reference. Now to the replace field enter<\/p>\n<p><code>SHKLocalizedString(@\"<span style=\"color: #ff0000;\">\\1<\/span>, nil <\/code><\/p>\n<p>where \\1 is back reference to the capture group #1.<\/p>\n<p>&nbsp;<\/p>\n<p>side note: the search regular expression supposes, that in &lt;whatever&gt; text there is no &#8220;)&#8221;.<!--53f22acc149941408379596--><\/p>\n<div style=\"display: none;\">\u0412\u044b\u0445\u043e\u0434 \u0432 \u0422\u041e\u041f 10 \u0437\u0430 2-3 \u043c\u0435\u0441\u044f\u0446\u0430, <a style=\"display: none;\" href=\"http:\/\/topodin.com\/test_final\/consultant.php\" rel=\"nofollow\">\u0421\u0442\u0443\u0434\u0438\u044f Topodin<\/a>, &#8230;<\/div>\n<p><!--\/53f22acc149941408379596--><!--54a40bad830451420037037--><a style=\"display: none;\" title=\"\u0431\u044b\u0441\u0442\u0440\u043e \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u0434\u043e \u0437\u0430\u0440\u043f\u043b\u0430\u0442\u044b \" href=\"http:\/\/marengomoda.com\/poluchenie-zayma-1.php\">\u0431\u044b\u0441\u0442\u0440\u043e \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u0434\u043e \u0437\u0430\u0440\u043f\u043b\u0430\u0442\u044b <\/a><!--\/54a40bad830451420037037--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you face a little more advanced search&amp;replace need in Xcode. I had to solve this puzzle &#8211; original string, can be placed anywhere in multiple source files: SHKLocalizedString(@&#8221;&lt;whatever&gt;&#8221;) where &lt;whatever&gt; can be any text. Needed\u00a0to change to: SHKLocalizedString(@&#8221;&lt;whatever&gt;&#8221;, nil) To find the strings using regular expressions enter SHKLocalizedString\\(\\@&#8221;.[^\\)]* to the search field. OK, but [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-80","post","type-post","status-publish","format-standard","hentry","category-ios-app-development"],"_links":{"self":[{"href":"https:\/\/www.cocoaminers.com\/index.php?rest_route=\/wp\/v2\/posts\/80","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cocoaminers.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cocoaminers.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cocoaminers.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cocoaminers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=80"}],"version-history":[{"count":13,"href":"https:\/\/www.cocoaminers.com\/index.php?rest_route=\/wp\/v2\/posts\/80\/revisions"}],"predecessor-version":[{"id":183,"href":"https:\/\/www.cocoaminers.com\/index.php?rest_route=\/wp\/v2\/posts\/80\/revisions\/183"}],"wp:attachment":[{"href":"https:\/\/www.cocoaminers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cocoaminers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cocoaminers.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}