Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In XCode 8, how do you use regex search replace to change the case of a character?

I inherited an Objective C project with a bunch of methods named things like "getMerchantSettings". Of course in Objective C (unlike Java) it is unconventional to use the word "get" in the method name for getters, since it makes them incompatible with auto-synthesized property getters, etc.

I can globally search my project to find all such offending method names using a regex pattern like get([A-Z]). In SublimeText I can replace all these with \L$1 and it will change everything like "getMerchantSettings" to just say "merchantSettings"; the \L will make the "M" character into the lower case "m" when it replaces it.

However in XCode's version of regex, \L and \l both do not work. What is the regex replacement pattern to use in XCode 8 to make it change the case of whatever it's replacing? Would prefer not to have to use a separate text editor for things like that. Thanks!

like image 477
CommaToast Avatar asked Oct 17 '16 19:10

CommaToast


People also ask

Can I use regex in replace?

The Regex. Replace(String, String, MatchEvaluator, RegexOptions) method is useful for replacing a regular expression match if any of the following conditions is true: If the replacement string cannot readily be specified by a regular expression replacement pattern.

How do I replace text in Xcode?

Xcode displays the results of the search, but it doesn't automatically replace the text. To replace the text for a single result, select it and click the Replace button. To replace the text for all results, click Replace All.


1 Answers

XCode 8 can't do that for you unfortunately. As you have perl, you can open a terminal, cd to your repo and run this:

find . -name *.[EXTENSION] -exec perl -i.bak -pe 's/get([A-Z])/\L$1/' {} \;

This will generate a backup of your files (.bak files) and change your getters to the needed format.

like image 147
Thomas Ayoub Avatar answered Oct 23 '22 12:10

Thomas Ayoub