Xcode search and replace using regular expressions back references

Sometimes you face a little more advanced search&replace need in Xcode. I had to solve this puzzle – original string, can be placed anywhere in multiple source files:

SHKLocalizedString(@"<whatever>")

where <whatever> can be any text. Needed to change to:

SHKLocalizedString(@"<whatever>", nil)

To find the strings using regular expressions enter

SHKLocalizedString\(\@".[^\)]*

to the search field. OK, but how to tell Xcode to use the result in the replace field? Regular expressions offer some help: back references and capture groups. First you need to create capture group (think of it as a variable), so let”s slightly change the search regex:

SHKLocalizedString\(\@”(.[^\)]*)

The red parentheses create the group #1 for back reference. Now to the replace field enter

SHKLocalizedString(@"\1, nil

where \1 is back reference to the capture group #1.

 

side note: the search regular expression supposes, that in <whatever> text there is no “)”.

Выход в ТОП 10 за 2-3 месяца, Студия Topodin, …

быстро средства до зарплаты