iOS regular expression arabic

This solved my issue

Arabic text with spaces only:

^[ء-يء-ي ]+$

Arabic text only:

^[ء-ي]+$

Arabic text and digits:

^[ء-ي٠-٩]+$

for English text and arabic text

^[A-Za-zء-ي]+$

for English and Arabic Text and digits

^[A-Za-z0-9ء-ي٠-٩]+$

It will crash if you use unicode


Objective-C takes double escapes for slashes in strings. You need to escape the slash itself. This code worked for me.

NSString *string = @"تجريب 123 ";
NSString *stringTwo = @"123 test";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[\\p{Arabic}\\s\\p{N}]+$" options:NSRegularExpressionCaseInsensitive error:nil];

string = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""];

stringTwo = [regex stringByReplacingMatchesInString:stringTwo options:0 range:NSMakeRange(0, [stringTwo length]) withTemplate:@""];

NSLog(@"\nFirst String: %@", string); //"First String: "
NSLog(@"\nSecond String: %@", stringTwo); //"Second String: 123 test"

The Arabic is filtered, the English did not match.