我有一个匹配_text_
的正则表达式,但不匹配\_text\_
[^\\_]_(.)*[^\\_]_
这似乎适用于在线RegExr解析工具。我尝试将其作为NSString无效。我有这个:@"[^\\\\_]_(.)*[^\\\\_]_"
任何指针都会有所帮助。
答案 0 :(得分:0)
你提供的字符串对我有用,但是在表达式开头的否定集应该有" *"量词?
NSString *goodString = @"_text_";
NSString *badString = @"\\_text\\_";
NSError *error;
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@"[^\\\\_]*_(.)*[^\\\\_]_"
options:0
error:&error];
NSLog(@"%@ - %lu", goodString, (unsigned long)[regex numberOfMatchesInString:goodString options:0 range:NSMakeRange(0, goodString.length)]);
NSLog(@"%@ - %lu", badString, (unsigned long)[regex numberOfMatchesInString:badString options:0 range:NSMakeRange(0, badString.length)]);
结果:
_text_ - 1
\_text\_ - 0