我有一个类似@"random++qwerty/asdf"的字符串,我想找出 qwerty 部分。 “random++”和“/asdf”总是相同的。我将如何使用正则表达式来执行此操作?我对它们的工作方式感到困惑。
最佳答案
问题解决了!!
这段代码正是你想要的,
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);
}];
输出:
qwerty
坏蛋
注意
关于ios - NSString 正则表达式字符串匹配/搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16204218/