I have a string like @"random ++qwerty/asdf" and I want to fish out the qwerty part. The "random ++" and "/asdf" will always be the same. How would I go about doing this using regular expressions? I'm confused as to how they work.
1 Answer
Problem Solved !!
This code give exactly what you want,
NSString *yourString = @"random ++qwerty/asdf random ++derty/asdf random ++noty/asdf";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"random \\+\\+(\\w+)/asdf" options:NSRegularExpressionCaseInsensitive error:&error];
[regex enumerateMatchesInString:yourString options:0 range:NSMakeRange(0, [yourString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
// detect
NSString *insideString = [yourString substringWithRange:[match rangeAtIndex:1]];
//print
NSLog(@"%@",insideString);
}];
Output:
qwerty
derty
noty
1 Comment
SaltyNuts
With so many resources available on regex usage, it still helps a lot to have concrete examples like this about specific types of matches! Thanks!
-substringWithRange:.BOOL isRightFormat = [string rangeOfString: @"random ++"].location == 0;