Hemang
Hemang

Reputation: 27072

How to test the UAE number in Arabic using RegEx?

func isValidUAENumber() -> Bool {
    let phoneRegExAr = "^(?:\\+٩٧١|٠(٠٩٧١)?)(?:[٢٣٤٦٧٩]|٥[٠١٢٥٦])[٠-٩]{٧}$"
    let phoneRegEx = "^(?:\\+971|0(0971)?)(?:[234679]|5[01256])[0-9]{7}$"
    let isArabic = true //(or false based on some condition)
    let useRegEx = isArabic ? phoneRegExAr : phoneRegEx
    let phonePredicate = NSPredicate(format:"SELF MATCHES %@", useRegEx)
    return phonePredicate.evaluate(with: self)
}

So this code is working fine with the UAE numbers in English and now I am trying to check the same with UAE numbers in Arabic. My mind suggested me to convert the english numbers RegEx to Arabic numbers RegEx so I used online translation tool to convert english to Arabic numbers and here I have the Arabic numbers RegEx but app will get crash as I have used the Arabic regex to check for a valid UAE number in Arabic:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Can't do regex matching, reason: Can't open pattern U_REGEX_BAD_INTERVAL (string +٩٧١٥٢٥٣٩٥٧٥٧, pattern ^(?:+٩٧١|٠(٠٩٧١)?)(?:[٢٣٤٦٧٩]|٥[٠١٢٥٦])[٠-٩]{٧}$, case 0, canon 0)'

Any one came across this situation before? Please help.

Update 1:

This is how Arabic RegEx looks in Xcode? enter image description here

Sample numbers:

  1. 072466666 (٠٧٢٤٦٦٦٦٦)
  2. +97172466666 (+٩٧١٧٢٤٦٦٦٦٦)

You may convert them to Arabic numbers to test?

Update 2:

After using the RegEx ("^(?:\\+٩٧١|٠(٠٩٧١)?)(?:[٢٣٤٦٧٩]|٥[٠١٢٥٦])[٠-٩]{7}$") which is provided by rmaddy it's stopped crashing but still it's not detecting whether it's a valid number or not.

Upvotes: 0

Views: 1247

Answers (1)

Nikolas
Nikolas

Reputation: 44486

There is described the Arabic script in Unicode (source: Wikipedia) and the range is from U+0600 to U+06FF so the regex such as:

([\u0600-\u06FF]+)

Should work. And it does (Regex101). For the digits only, use the range between U+0660 and U+0669.

Upvotes: 2

Related Questions