7

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!

Community
  • 1
  • 1
Ted
  • 22,696
  • 11
  • 95
  • 109
  • possible duplicate of [How to match arabic words with reg exp?](http://stackoverflow.com/questions/15465760/how-to-match-arabic-words-with-reg-exp) – VladL May 20 '15 at 16:42
  • `\u0621` is generically UTF16, with the same value in UTF-32. That means the engine must support code points `>= 0x100`. It seems strange that if it accept the utf-16 constructs, it doesn't auitomatically promote the regex to Unicode. Check for a flag to make it Unicode regex. –  May 20 '15 at 16:43
  • Update your question with the actual code giving your the error. – rmaddy May 20 '15 at 16:48
  • 1
    Remember that you must also escape `\ ` characters in the string, in addition to the regex. Have you tried `^[\\p{Arabic} ]+$`? – JDB May 20 '15 at 17:09
  • \ in \\p{Arabic} solved the crashing. Thanks for that! However for ^[\u0621-\u064A]+$, how do we actually use this? – Ted May 21 '15 at 05:22

2 Answers2

9

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

Ted
  • 22,696
  • 11
  • 95
  • 109
  • i want to add special character, number and arabic text. I am using likelet passwordRegex = "^(?=.*[\\p{L}&&[\\p{script=Arabic}A-Za-z]])(?=.*[\\p{N}&&[\\p{script=Arabic}0-9]])[\\p{L}\\p{N}\\p{P}\\p{S}&&[\\p{script=Arabic}a-zA-Z0-9]]{8,}$" – Chandan Jee Jul 17 '20 at 13:51
  • can you please help me? – Chandan Jee Jul 17 '20 at 13:51
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.

Dare
  • 2,497
  • 1
  • 12
  • 20
  • At this point it's an assumption that the OP is using Objective-C. – rmaddy May 20 '15 at 17:54
  • \ in \\p{Arabic} solved the crashing. Thanks for that! However for ^[\u0621-\u064A]+$, how do we actually use this? – Ted May 21 '15 at 05:21