iOS正则表达式阿拉伯语

问题描述 投票:5回答:2

我来自这个帖子

Regular Expression Arabic characters and numbers only

How to match arabic words with reg exp?(没有回答我的问题)

我试过了

^[\p{Arabic} ]+$

并收到

'解析错误',原因:'无效的转义序列@ pos 3:^ [\▶p {阿拉伯语}] + $'

我也试过了

^[\u0621-\u064A\s]+$

'解析错误',原因:'字符范围无效@ pos 7:^ [\ u062▶1- \ u064A \ s] + $'

我也试过了

^[\u0621-\u064A]+$

'解析错误',原因:'无效的字符范围@ pos 7:^ [\ u0622▶1- \ u064A] + $'

我需要^ [A-Za-z] + $接受阿拉伯字符。

提前致谢!

ios regex localization arabic nsregularexpression
2个回答
3
投票

这解决了我的问题

只有空格的阿拉伯文字:

^ [E-y] + $

仅阿拉伯文:

^ [-A] + $

阿拉伯文字和数字:

^ [-A-9] + $

用于英文文本和阿拉伯文

^ [A-Za-z] + +

英语和阿拉伯语文本和数字

^ [A-Za-z0-9 + -0-9] + $

崩溃是因为我正在使用unicode ..对不起。


2
投票

Objective-C对字符串中的斜杠采用双重转义。你需要逃避斜线本身。这段代码对我有用。

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"

阿拉伯语被过滤,英语不匹配。

© www.soinside.com 2019 - 2024. All rights reserved.