I need to find all the strings in my XCode project. I know that I can use grep and regex to accomplish this, but am well versed in neither.
The pattern I want to find is anything on one line that starts with '@"' and ends with '"'. I might throw in a minimum of 5 or so characters in between, also.
So for instance, if I searched through the following code:
NSArray *array = @[@"this is the first", @"this is the second"];
for (NSString* thisString in array)
{
NSLog(@"%@", thisString);
}
Only "this is the first" and "this is the second" would be hits. Am I on the right track with using regex, or is there another technique that would be more suitable for this?
Thanks!
You can search in the current file if you click on “Find -> Find…” or by using the keyboard shortcut Command + F. Then a search bar appears and you can enter a search string. The global search can be accessed through the “Find navigator” in the “Navigator” on the left side.
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.
Enter the files name in the bottom bar (search field) of the Project Navigator (on the left side), press enter => there it is ;-) Fantastic!
Regular expressions are just strings themselves. Each character in a regular expression can either be part of a code that makes up a pattern to search for, or it can represent a letter, character or word itself.
Regex is fine for these sorts of searches. Here are a few quick 'n' dirty alternatives:
@".*?"
- will find any occurrences including the quotes.@".{5,}?"
- does the same, but minimum five characters.(?<=@").*?(?=")
- if you want to exclude the quotesThese won't handle escaped quotation marks within the string, nor strings that spread across multiple lines (notably when the subsequent lines omit the leading @
). It may also match occurrences in comments (not just code). It may also have problems if you have mismatched quotes (e.g. in your code comments).
If you're just quickly searching for strings, these regex strings might help. If you're trying to automate some replacement, some greater care is called for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With