2012-08-12 43 views
0
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)为什么表达式仍然是零?static NSRegularExpression always nil

+0

很容易,因为这个'<(?!br|p)+[^>] * >'正则表达式是无效的,因此你得到一个'nil'指针。你应该做一个正确的正则表达式,你会得到一个有效的对象。 – holex 2012-08-12 08:14:12

+0

谢谢@holex – 2012-08-12 08:23:50

+0

我已将其修正为<(!!/?(br | p | i | b | strong))[^>] *> – 2012-08-12 08:27:41

回答

3

你永远不分配一个NSRegularExpression对象,所以你打电话initnil这保证返回nil

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

将其更改为:

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

谢谢,但它仍然为零。我添加了代码“if(isRichContentRegex == nil)NSLog(@”still nil“);” ,它已经印制了“仍然没有”。 – 2012-08-12 08:14:34

+0

看到你的问题的评论(通过@holex) – MByD 2012-08-12 08:15:50

+0

是的,它是'nil',因为'<(?!br|p)+[^>] *>'正则表达式是错误的,因此替换init方法不是解决这个问题的方法。 :( – holex 2012-08-12 08:16:27