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.
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
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