Xcode renaming is good, but in some cases it can’t help.
Often, I go back to using Atom to find and replace, using regex.
1. Find
- Open a file (or a directory if multiple files) in Atom
CMD+C
to copy the string to findCMD+SHIFT+F
to bring up Find panel
When you bring up Atom’s Find panel, the copied string will be automatically populated in the find field, and best of all – automatically escape regex characters!
See the \
added in the Find textfield:
disease\.cure\(with: "vaccine"\)
.
NOTE: Make sure the regex mode (the .*
option) is enabled.
2. Captured Group
Using the following example, we want to find some functions with string, and refactor them to a simple form:
// Original
disease.cure(with: "vaccine")
disease.cure(with: "mask")
// To replace and become
vaccine.asCure
mask.asCure
To do that, we need to add captured group for any strings (vaccine, mask, etc). We do that with (.*?)
, capturing the shortest possible string.
We edit Find to:
disease\.cure\(with: "(.*?)"\)
3. Replace
Finally, we replace with:
$1.asCure
The first captured group is $1
. If you have more captured groups, you can use $2
, $3
etc.