Reputation: 309
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!
Upvotes: 7
Views: 5655
Reputation: 2361
As of writing this using Xcode 12.5 you can simply enter an Any
pattern in between two "
in the search bar by clicking the magnifying glass. ⤵︎
Upvotes: 10
Reputation: 437632
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.
Upvotes: 23