Context
I've been going back & forth for a couple of hours trying to solve something and am still not satisfied with the result.
I want to get the username of an iPhone from UIDevice both in English and Spanish. So I've come up with a regex to get ("Andres's iPhone" > "Andres") and ("iPhone de Andres" > "Andres").
The magic is something like:
"(?:iPhone|iphone|phone|iPad|ipad|pad|iPod|ipod|pod)(?:\\s+\\S+)*\\s+de\\s+(.+)|"
"(.+)'s\\s+(?:iPhone|iphone|phone|iPad|ipad|pad|iPod|ipod|pod)(?:\\s+\\S+)*"
Problem
When trying to get the substring from [[UIDevice currentDevice] name] the user name can be matched by one of the two alternatives in the regex.
This causes that the actual name might be obtained by:
[[regEx firstMatchInString:str options:0 range:NSMakeRange(0, [str length])] rangeAtIndex:1]
if the first alternative matched; but if the second alternative performs the match, then it is:
[[regEx firstMatchInString:str options:0 range:NSMakeRange(0, [str length])] rangeAtIndex:2]
Notice that, according to the alternative matching, I have to get the first index or the second. For the moment I've worked around this with:
+ (NSString *)getSubstringInString:(NSString *)str matchedByRegEx:(NSRegularExpression *)regEx
{
NSString * result = nil;
NSTextCheckingResult * match = [regEx firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];
for (int i=1; i<[match numberOfRanges]; i++) {
NSRange matchRange = [match rangeAtIndex:i];
if (!NSEqualRanges(matchRange, NSMakeRange(NSNotFound, 0))) {
result = [str substringWithRange:matchRange];
}
}
return result;
}
Actual question
But is there a better way to get the matching substring, disregarding what was the alternative from the regex used ?
Hope I've made myself clear, thanks in advance for any possible help!