static NSRegularExpression总是为零

时间:2012-08-12 07:59:02

标签: objective-c ios regex nsregularexpression

static NSRegularExpression *isRichContentRegex;
static NSError *regexError = NULL;

@implementation MkContentUtils

+(void)initialize{
if(isRichContentRegex == nil)
{
    isRichContentRegex = [isRichContentRegex initWithPattern:@"<(?!br|p)+[^>]*>"         options:NSRegularExpressionCaseInsensitive error:&regexError];
//      isRichContentRegex = [NSRegularExpression regularExpressionWithPattern:@"            (?!br|p)+[^>]*>" 
//                                                                     options:NSRegularExpressionCaseInsensitive
//                                                                      error:NULL];
    NSLog(@"isrichcontent_pattern:%@",isRichContentRegex.pattern);
}

打印日志是:isrichcontent_pattern :( null)为什么表达式仍为零?

1 个答案:

答案 0 :(得分:3)

您永远不会分配NSRegularExpression个对象,因此请致电init nil,保证返回nil

isRichContentRegex = [isRichContentRegex initWithPattern:@"<(?!br|p)+[^>]*>"         options:NSRegularExpressionCaseInsensitive error:&regexError];

将其更改为:

isRichContentRegex = [[NSRegularExpression  alloc] initWithPattern:@"<(?!br|p)+[^>]*>"         options:NSRegularExpressionCaseInsensitive error:&regexError];