iOS regular expression arabic

こ雲淡風輕ζ 提交于 2019-12-01 02:15:28

问题


I come from this posts

Regular Expression Arabic characters and numbers only

How to match arabic words with reg exp? (didn't answer my question)

I've tried

^[\p{Arabic} ]+$

and received

'Parse error', reason: 'Invalid escape sequence @ pos 3: ^[\ ▶p{Arabic} ]+$'

I've also tried

^[\u0621-\u064A\s]+$

'Parse error', reason: 'Invalid character range @ pos 7: ^[\u062 ▶1-\u064A\s]+$'

I've also tried

^[\u0621-\u064A]+$

'Parse error', reason: 'Invalid character range @ pos 7: ^[\u062 ▶1-\u064A]+$'

I need ^[A-Za-z ]+$ that accepts arabic characters.

Thanks in advance!


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/30355526/ios-regular-expression-arabic

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