ios - NSString 正则表达式字符串匹配/搜索

标签 ios objective-c regex nsstring

我有一个类似@"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/

相关文章:

ios - 如何彻底测试使用 Swift 创建的 iOS 应用程序?

ios - 旋转 CCSprite Cocos2D?

ios - 位置权限对话框在 iOS 10 中提示很多时间

c++ - Xcode 变量的类型不完整 void

regex - 正则表达式条件否定的.htaccess中的RewriteCond无法正常工作?

ios - 应用程序 :openURL:sourceApplication:annotation return NO versus YES

ios - ReactiveCocoa-映射方法错误

objective-c - 添加一个( double )变量到 NSMutableArray

regex - bash - 从右侧替换目录字符串的简单方法

javascript - 正则表达式匹配括号正则表达式内的引号