Ios7 NSDiacriticInsensitiveSearch和阿拉伯语搜索

Ios7 NSDiacriticInsensitiveSearch和阿拉伯语搜索,ios7,sqlite,uisearchbar,arabic,Ios7,Sqlite,Uisearchbar,Arabic,众所周知,NSDiacriticInsensitiveSearch对阿拉伯语字母的影响与对法语字母的影响不同。这就是为什么我试图用阿拉伯字母创造同样的效果。例如,如果用户输入字母“ا”,搜索栏应该同时显示包含字母“ا”和字母“أ”的所有单词。 以下行的使用: text = [text stringByReplacingOccurrencesOfString:@"ا" withString:@"أ"]; 不会显示以“ا”开头的单词的结果 在搜索栏中,我尝试实现与在法语案例中相同的NSDiac

众所周知,NSDiacriticInsensitiveSearch对阿拉伯语字母的影响与对法语字母的影响不同。这就是为什么我试图用阿拉伯字母创造同样的效果。
例如,如果用户输入字母“ا”,搜索栏应该同时显示包含字母“ا”和字母“أ”的所有单词。
以下行的使用:

  text = [text stringByReplacingOccurrencesOfString:@"ا" withString:@"أ"];
不会显示以“ا”开头的单词的结果
在搜索栏中,我尝试实现与在法语案例中相同的NSDiacriticInsensitiveSearch方法,但没有成功:

NSRange nameRange = [author.name rangeOfString:text options:NSAnchoredSearch | NSDiacriticInsensitiveSearch];

您知道如何完成此操作吗?

您可以使用正则表达式处理阿拉伯语(Alif)不同的形状

假设您有一个上下文,即“strong>”和“strong>”,并且要搜索的模式是“strong>”,那么您可以将处理“strong”之间差异的正则表达式转换为“strong>”。正则表达式应该是“(أ1243; 1245;)بب(أ1243;ӝ)هيم”。这将通过所有可能的形状搜索图案

下面是我编写的一个简单代码:

#import <Foundation/Foundation.h>

NSString * arabify(NSString * string)
{
    NSRegularExpression * alifRegex = [NSRegularExpression regularExpressionWithPattern:@"(أ|ا|إ)" options:0 error:nil];
    return [alifRegex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@"(أ|ا|إ)"];
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        NSString * context = @"محمد بن إبراهيم الابراهيمي";
        NSString * pattern = @"إبراهيم";

        // Get the regex for the Arabic word.
        NSString * regex = arabify(pattern);

        NSLog(@"context = %@", context);
        NSLog(@"pattern = %@", pattern);
        NSLog(@"regex = %@", regex);

        NSRange range = [context rangeOfString:regex options:NSRegularExpressionSearch];

        if (range.location == NSNotFound)
        {
            NSLog(@"Not found.");
        }
        else
        {
            NSLog(@"Found.");
            NSLog(@"location = %lu, length = %lu", (unsigned long)range.location, (unsigned long)range.length);
        }
    }

    return 0;
}
#导入
NSString*arabify(NSString*string)
{
NSRegularExpression*alifRegex=[NSRegularExpression regular expression with pattern:@“(أ124;ا|إ)”选项:0错误:无];
返回[alifRegex stringbyreplacingmatcheinstall:string选项:0范围:NSMakeRange(0,[字符串长度])和模板:@“(أ||إ)”;
}
int main(int argc,const char*argv[]
{
@自动释放池{
NSString*context=@“上下文”;
NSString*模式=@“إباهيم”;
//获取阿拉伯语单词的正则表达式。
NSString*regex=arabify(模式);
NSLog(@“context=%@”,context);
NSLog(@“模式=%@”,模式);
NSLog(@“regex=%@”,regex);
NSRange range=[字符串的上下文范围:正则表达式选项:NSRegularExpressionSearch];
if(range.location==NSNotFound)
{
NSLog(@“未找到”);
}
其他的
{
NSLog(@“找到”);
NSLog(@“位置=%lu,长度=%lu”,(无符号长)range.location,(无符号长)range.length);
}
}
返回0;
}

祝你好运,兄弟。

看来你使用的是复合符号(U+0623),它与Alif的其他表示法不一致

你考虑过Alif的其他编码方法吗?您可以使用分解后的变量,然后将其与“普通”Alif(U+0627)进行对比,这正是您想要的:

ARABIC LETTER ALEF (U+0627) ARABIC HAMZA ABOVE (U+0654)
请看这里: