itechnician
itechnician

Reputation: 1655

Cocoa Error 2048 on NSRegularExpression Creation

I am trying to create regular expression from the following:

#define RegEX_1_UPPER_1_SPECIAL @"((?=.*[A-Z])(?=.*[$@!%*?&-+_()]).{8,})"

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: RegEX_1_UPPER_1_SPECIAL options:0 error:&error];

But, unfortunately it returns me the following error:

Error Domain=NSCocoaErrorDomain Code=2048 "The operation couldn’t be completed. (Cocoa error 2048.)" UserInfo=0x7ff6f51b5af0 {NSInvalidValue=(?=.*[A-Z])(?=.*[$@!%*?&-+_]).{8,15}}

Upvotes: 2

Views: 265

Answers (1)

Rob
Rob

Reputation: 437432

The problem is the hyphen inside your collection of special characters. The hyphen has a special meaning within the [ and ], namely a range of characters (e.g. [A-Z]). If you want to search for a literal hyphen, you have to escape that:

#define RegEX_1_UPPER_1_SPECIAL @"((?=.*[A-Z])(?=.*[$@!%*?&\\-+_()]).{8,})"

Upvotes: 1

Related Questions