Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS regular expression arabic

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!

like image 931
Ted Avatar asked May 20 '15 16:05

Ted


2 Answers

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

like image 117
Ted Avatar answered Nov 14 '22 17:11

Ted


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.

like image 20
Dare Avatar answered Nov 14 '22 16:11

Dare