4

I'm building a regular expression for use in a parser in an iOS app. Here's my code:

NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:@"(?<=#EXT[^\r\n]*[\r\n]+)[^#][^\r\n]+"
                                          options:NSRegularExpressionAnchorsMatchLines
                                            error:&regexError
 ];
if (regexError) {
    NSLog(@"regexError: %@", regexError);
    return nil;
}

From this answer.

This gives out this error:

regexError: Error Domain=NSCocoaErrorDomain Code=2048 "The operation couldn’t be completed. (Cocoa error 2048.)" UserInfo=0x8e86670 {NSInvalidValue=(?<=#EXT[^

Cocoa error 2048 is an NSFormattingErrorMinimum according to the docs... But there's literally no further explanation.

What does it mean?

Community
  • 1
  • 1
Eric
  • 16,003
  • 15
  • 87
  • 139

1 Answers1

3

are you trying to match a new line/line feed character? you've inserted a literal new line character into your regex... you need to instead insert the code for a newline. Try escaping as \\n etc.

edit:

You have to escape all special strings. For example you want your regex string to contain \+r, not a linefeed character. So you need to use \\r instead of \r.

i.e.

"(?<=#EXT[^\\r\\n]*[\\r\\n]+)[^#][^\\r\\n]+"

edit 2:

You cannot have unlimited length strings in your look-behind. So, no * and no + allowed. This is per the ICU regex reference. (NSRegularExpression uses ICU regex syntax.)

nielsbot
  • 15,922
  • 4
  • 48
  • 73
  • Yes, I am trying to match a new line character. I tried this: `@"(?<=#EXT[^\r\\n]*[\r\\n]+)[^#][^\r\\n]+"` but I got the same error. – Eric Dec 17 '13 at 17:29
  • You have to escape all the special characters, now just `\n`. I updated my answer. – nielsbot Dec 17 '13 at 19:33
  • Turns out your look-behind is invalid. Do it without look-behind. – nielsbot Dec 17 '13 at 22:28
  • I tried this: `#EXT[^\\r\\n]*[\\r\\n]+[^#][^\\r\\n]+`. The error went away, but now it doesn't match my strings.. – Eric Dec 18 '13 at 09:22
  • 1
    Well I answered the error question :) now you need to debug your regex. I use Reggy : https://www.macupdate.com/app/mac/24078/reggy – nielsbot Dec 18 '13 at 09:33