1

I have a regex to check Max 70 alphanumeric characters and special characters: ' / \\ - ; @ and space for which i am using the following regex pattern -

^[a-zA-Z0-9,.-\s'\\\/@]{0,70}$

Please Note: testing this in https://regex101.com works perfectly fine

And the following code to match it with string -

NSString *String = area@123\\
NSRegularExpression *regex  = [NSRegularExpression regularExpressionWithPattern:@"^[a-zA-Z0-9,.-\s'\\\/@]{0,70}$" options:NSRegularExpressionCaseInsensitive error:&error];

NSAssert(regex, @"Unable to create regular expression");
NSRange textRange   = NSMakeRange(0, string.length);
NSRange matchRange  = [regex rangeOfFirstMatchInString:string options:NSMatchingReportProgress range:textRange];

Initially it was showing escape sequence error for which I changed the pattern to - ^[a-zA-Z0-9,.-\\s'\\\\//@]{0,70}$

And now its resulting in crash with message-

Assertion failure in +[UMValidations validateString:withPattern:]
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to create regular expression

Now what wrong is happening when this pattern works great on the regex tester.

4
  • Please provide the string literal with the regex you are using. Commented Aug 19, 2016 at 11:32
  • The problem most likely is due to the backslahes. There are several levels of escaping at work: Objective-C, regular expression and literal use. Therefore it's important you show how the expression appears in the code. Commented Aug 19, 2016 at 11:35
  • Just escape the dash or put it at the begining or at the end of the character class. Commented Aug 19, 2016 at 11:36
  • Hi. I have updated my code structure. Please check. Commented Aug 19, 2016 at 11:38

1 Answer 1

2

When you write regex patterns in Objective-C, the backslashes with special chars must be doubled. Besides, you need to also escape the hyphen (or place it at the start/end of the character class).

@"^[a-zA-Z0-9,.\\-\\s'\\\\/@]{0,70}$"
               ^^ ^^  ^^^^

You do not need to escape /. You need to use 4 backslashes to define a literal \ because the regex engine uses a literal backslash (in an Objective-C string literal defined as "\\") to escape special characters and to denote a literal \, the literal backslash in the Objective-C string literal must be doubled (\\ in a literal string pattern (so, defined as "\\\\") will match a literal \ in the input). The main point is that the \ in the string literal can form an escape sequence like "\n" (newline) or "\r" (carriage return), etc. So, there are two layers of escaping: one for the Objective-C engine, and the other - for the regex (ICU in this case) library.

See the Objective-C demo:

NSString *String = @"area@123\\";
NSRegularExpression *regex  = @"(?i)^[a-z0-9,.\\-\\s'\\\\/@]{0,70}$";
NSPredicate * Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if ([Test evaluateWithObject:String]) {
    NSLog (@"yes");
} else {
    NSLog (@"no");
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please explain the mechanism behind this - You need to use 4 backslashes to define a literal \. Thanks
simply in C string literals "\" has spatial meaning. To provide this character in string literal you have to use "\\". In regular expression this character also has spatial meaning so you end up with "\\\\" just to match single slash.
I added some more explanation with a small demo. Note that I removed A-Z just because the case insensitive modifier (?i) is used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.