7

I want to detect Arabic or Persian character in a string.

For example:

search in string = "مشخصات، قیمت و خرید لپ تاپ 15 اینچی ایسر مدل Aspire ES1-533-C4UH"

and return true

and search in string="Aspire ES1-533-C4UH"

and return false

string pattern = @"^[\p{IsArabic}\s\p{N}]+$";
string input = @"مشخصات، قیمت و خرید لپ تاپ 15 اینچی ایسر مدل Aspire ES1-533-C4UH";
RegexOptions options = RegexOptions.RightToLeft;"

foreach (Match m in Regex.Matches(input, pattern, options))
{
    if(m.Value !="")
    {
        bool x=true;
    }
    else
        x=false;
}

but this doesn not work.

Koby Douek
  • 16,156
  • 19
  • 74
  • 103
HIV88
  • 157
  • 1
  • 9

1 Answers1

9

Try using this (I'm using it and it works).

This Regex accepts all arabic letters by UTF ranges.

Regex regex = new Regex("[\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufc3f]|[\ufe70-\ufefc]");
return regex.IsMatch(text);
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
  • 1
    Just curious if you have any experience using unicode blocks, and if this is better or worse? e.g., `\p{IsArabic}` https://learn.microsoft.com/en-us/dotnet/standard/base-types/character-classes-in-regular-expressions#SupportedNamedBlocks – BurnsBA Aug 30 '17 at 13:44
  • 1
    +1. Also see [this](https://stackoverflow.com/a/11323651/21567) for a "little" more information (albeit it is Javascript). – Christian.K Aug 30 '17 at 13:46
  • thanks @BurnsBA ,@Christian.K,@AdrianoRepetti,@Koby Douek. good work my friend,dear koby. – HIV88 Aug 30 '17 at 13:49
  • @HIV88 Happy to help brother. – Koby Douek Aug 30 '17 at 14:07
  • Thanks for your hepl @KobyDouek :* – HIV88 Sep 04 '17 at 10:52