Ryan Copley
Ryan Copley

Reputation: 873

Cocoa Error 2048 on NSRegularExpression

I'm attempting to use NSRegularExpression to search for a string inside a pbxproj (Inside the .xcodeproj folder).

I'm searching for the compiler flags in the "Begin PBXBuildFile section" area

NSString* findFlagsRegex = @"([A-Z0-9]{24}\\s\\/\\*\\s[A-Za-z\\.\\s0-9]+\\*\\/\\s=\\s{isa\\s\\=\\s[A-Za-z]*;\\s?fileRef\\s\\=\\s[A-Z0-9]*\\s\\/\\*\\s[A-Za-z0-9\\s\\.]*\\*\\/;\\ssettings\\s\\=\\s{[A-Za-z0-9_\\s\\=\"-]*;\\s\\};\\s};)";
NSRegularExpression* expression3 = [NSRegularExpression regularExpressionWithPattern:findFlagsRegex options:kNilOptions error:&err];
NSLog(@"Error: %@",[err description]);

Error Domain=NSCocoaErrorDomain Code=2048 "The value “([A-Z0-9]{24}\s\/\*\s[A-Za-z\.\s0-9]+\*\/\s=\s{isa\s\=\s[A-Za-z]*;\s?fileRef\s\=\s[A-Z0-9]*\s\/\*\s[A-Za-z0-9\s\.]*\*\/;\ssettings\s\=\s{[A-Za-z0-9_\s\="-]*;\s\};\s};)” is invalid." UserInfo=0x61800026a7c0 {NSInvalidValue=([A-Z0-9]{24}\s\/\*\s[A-Za-z\.\s0-9]+\*\/\s=\s{isa\s\=\s[A-Za-z]*;\s?fileRef\s\=\s[A-Z0-9]*\s\/\*\s[A-Za-z0-9\s\.]*\*\/;\ssettings\s\=\s{[A-Za-z0-9_\s\="-]*;\s\};\s};)}

I copy:

  ([A-Z0-9]{24}\s\/\*\s[A-Za-z\.\s0-9]+\*\/\s=\s{isa\s\=\s[A-Za-z]*;\s?fileRef\s\=\s[A-Z0-9]*\s\/\*\s[A-Za-z0-9\s\.]*\*\/;\ssettings\s\=\s{[A-Za-z0-9_\s\="-]*;\s\};\s};)

The regular expression above works in RegexPal, directly copying it from the invalid value from the error message on the same test data... so I'm not sure what's wrong :/

Not sure if this will add anything, but this is a Mac App and not an iOS app.

Upvotes: 1

Views: 1320

Answers (1)

ArtOfWarfare
ArtOfWarfare

Reputation: 21478

Your pattern contains a lone literal }. I believe you meant to have two literal {s and two literal }s - this is a slightly modified version of the pattern you had in your question, with three more \\s inserted to escape the curly braces that are currently not escaped in your code.

NSString* findFlagsRegex = @"([A-Z0-9]{24}\\s\\/\\*\\s[A-Za-z\\.\\s0-9]+\\*\\/\\s=\\s\\{isa\\s\\=\\s[A-Za-z]*;\\s?fileRef\\s\\=\\s[A-Z0-9]*\\s\\/\\*\\s[A-Za-z0-9\\s\\.]*\\*\\/;\\ssettings\\s\\=\\s\\{[A-Za-z0-9_\\s\\=\"-]*;\\s\\};\\s\\};)";

I'm not sure whether the bug is with RegexPal, or if RegexPal depends on the copy of JS that your browser uses, or if the bug is with NSRegularExpressions, but either way, escaping a character which doesn't need to be escaped shouldn't cause any issues (or at least it's not my understanding of regular expressions that it should.)

Upvotes: 2

Related Questions