2012-08-12 70 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