I want to check whether string contain arabic text or not.
I tried this all answers but nothing is working.
Can some one let me know what regular expression should I use?
nickNameTF.text = @"eweسبيب";
NSLog(@"nickNameTF.textnickNameTF.text===%@", nickNameTF.text);
NSString *emailRegex = @"\p{Arabic}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
if ([emailTest evaluateWithObject:nickNameTF.text]) {
DTAlertView *myAl = [DTAlertView alertViewWithTitle:localize(@"myErr") message:localize(@"srNicc") delegate:nil cancelButtonTitle:localize(@"dismiss") positiveButtonTitle:nil];
[myAl setDismissAnimationWhenButtonClicked:DTAlertViewAnimationSlideTop];
[myAl showWithAnimation:DTAlertViewAnimationSlideBottom];
nickNameTF.enabled = YES;
nickNameTF.userInteractionEnabled = YES;
}
nickNameTF.text = @"يسيس";
// nickNameTF.text = @"adsيسيس";
// nickNameTF.text = @"ads";
NSLog(@"nickNameTF.textnickNameTF.text===%@", nickNameTF.text);
NSString *emailRegex = @"\\p{Arabic}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
if (![emailTest evaluateWithObject:nickNameTF.text]) {
NSLog(@"not trueeeee");
}
if ([emailTest evaluateWithObject:nickNameTF.text]) {
NSLog(@"trueeeee");
}
Following the documentation, the code should be written as:
NSPredicate *emailTest =
[NSPredicate predicateWithFormat: @"SELF MATCHES %@", @"'\\\\p{Arabic}'"];
Your current code is missing the single quotes around the regular expression. You also need to double escape \
, since there are 2 layers of escape: one is string literal syntax of Objective-C, and the other is string literal syntax of NSPredicate.
Since you want to check whether the string contains an Arabic character, the regex should be
(?s).*\p{Arabic}.*
(?s)
is an inline flag which makes .
matches any code point without exception.
Plugging it in the code above:
NSPredicate *emailTest =
[NSPredicate predicateWithFormat: @"SELF MATCHES %@", @"'(?s).*\\\\p{Arabic}.*'"];
In predicate format string syntax, only MATCHES
works with regex, and it asserts that the whole string matches the regex, so the regex needs to be written to match the whole string.
Alternatively, you can use firstMatchInString:options:range:
in NSRegularExpression.
In this method, you only need to escape once in Objective-C string literal. Since the method will search for a matching substring, your regex doesn't have to be written to match the whole string.
Modifying from the example code in the documentation:
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\p{Arabic}"
options:0
error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:nickNameTF.text
options:0
range:NSMakeRange(0, [nickNameTF.text length])];
if (match) {
// If there is an Arabic character
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With