【发布时间】:2016-08-24 15:40:11
【问题描述】:
我尝试删除括号内的部分字符串。
例如,对于字符串"(This should be removed) and only this part should remain",在使用NSRegularExpression 后它应该变成"and only this part should remain"。
我有这段代码,但没有任何反应。我已经用 RegExr.com 测试了我的正则表达式代码,它工作正常。我将不胜感激。
NSString *phraseLabelWithBrackets = @"(test text) 1 2 3 text test";
NSError *error = NULL;
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"/\\(([^\\)]+)\\)/g" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *phraseLabelWithoutBrackets = [regexp stringByReplacingMatchesInString:phraseLabelWithBrackets options:0 range:NSMakeRange(0, [phraseLabelWithBrackets length]) withTemplate:@""];
NSLog(phraseLabelWithoutBrackets);
【问题讨论】:
标签: objective-c regex nsregularexpression