iOS regular expression arabic

隐身守侯 提交于 2019-12-01 04:57:42

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!