NSPredicate使用核心数据,在字符串属性中搜索带有边界的单词 [英] NSPredicate with core data, search word with boundaries in string attribute

查看:76
本文介绍了NSPredicate使用核心数据,在字符串属性中搜索带有边界的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我的问题的答案可能已经在这个论坛上的某个地方,但是经过数小时的搜索后,我似乎还是不太正确。我以前从未使用过SQL,所以请多多包涵。



我有一个只有一个实体的核心数据模型,并且该实体有一个称为 definitions的属性(每个数据库中的实体是一个字典条目)。我想做的是在定义中搜索带有单词边界的输入字符串。如果我使用NSPredicate和输入文本进行简单的 CONTAINS [cd] 搜索,我将得到所需的内容,除了我还将得到包含输入顺序为



所以我也尝试过构建正则表达式字符串,例如 @(?w)。* \\ btheWord\\b。* ,以及 MATCHES [cd] 搜索命令,但我读过其他文章,这些文章只能



这应该很简单,但是我很困惑。感谢所有帮助。



编辑:这是我在做什么的示例

  YTAppDelegate *委托= [[UIApplication sharedApplication]委托]; 

NSManagedObjectContext * context =委托.managedObjectContext;

NSFetchRequest *请求= [NSFetchRequest fetchRequestWithEntityName:@ YTDictData];


NSString * searchTerm = [NSString stringWithFormat:@'(?w)。* \\b%@ \\b。*',[NSRegularExpression escapedPatternForString:输入文本]];

NSString * searchString = [NSString stringWithFormat:@定义包含[cd]%@,searchTerm];

request.predicate = [NSPredicate predicateWithFormat:searchString];

NSError * searchError = nil;

NSArray * searchResults = [上下文executeFetchRequest:request错误:& searchError];

在这种情况下,如果我要搜索单词 hello,则不会找到匹配项



但是,如果我不格式化 searchString 字符串以进行正则表达式搜索(即 searchString = inputString ),我将获得任何包含字符 hello序列的定义。我需要能够找到包含字符串 hello的字符串,但是我还需要增加约束条件,使 hello被识别为带有边界的单词。

进行正则表达式匹配。此外,您不应使用
stringWithFormat 来构建谓词字符串(您的 searchString



这应该是这样的:

  NSString * searchTerm = [NSString stringWithFormat:@。* \\b%@\\b.*,
[NSRegularExpression escapedPatternForString:inputText]];
request.predicate = [NSPredicate predicateWithFormat:@ definitions MATCHES [cd]%@,
searchTerm];


I feel like the answer to my question might already be somewhere on this forum, but after hours of scouring I just can't quite seem to get it right. I've never used SQL before so please bear with me.

I have a core data model with only one entity and that entity has an attribute called "definitions" (each entity in the database is a dictionary entry). What I want to do is search the definitions for an input string with word boundaries. If I do a simple CONTAINS[cd] search with NSPredicate and the input text I get what I'm looking for, except that I also get definitions that contain the input sequence of characters, regardless of boundaries.

So I've also tried building a regex string like @"(?w).*\\btheWord\\b.*", along with the MATCHES[cd] search command, but I read on other posts that can only be used when searching NSStrings, and regardless it didn't work.

This should be simple but I'm stumped. Any help appreciated.

EDIT : Here's an example of what I'm doing

YTAppDelegate * delegate = [[UIApplication sharedApplication] delegate];

NSManagedObjectContext * context = delegate.managedObjectContext;

NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@"YTDictData"];


NSString * searchTerm = [NSString stringWithFormat:@"'(?w).*\\b%@\\b.*'",[NSRegularExpression escapedPatternForString:inputText]];

NSString * searchString = [NSString stringWithFormat:@"definitions contains[cd] %@",searchTerm];

request.predicate = [NSPredicate predicateWithFormat:searchString];

NSError * searchError = nil;

NSArray * searchResults = [context executeFetchRequest:request error:&searchError];

In this case, if I were to search for the word "hello", I would get no matches.

However if I do not format the searchString string for regex searching (ie searchString = inputString), I will get any definition that contains the sequence of characters "hello". I need to be able to find strings which contain the string "hello", but I also need the added constraint that I want "hello" to be recognized as a word with boundaries.

解决方案

As already said in the comments, you have to use the "MATCHES" operator for regular expression matching. In addition, you should not use stringWithFormat to build the predicate string (your searchString variable).

This is how it should work:

NSString *searchTerm = [NSString stringWithFormat:@".*\\b%@\\b.*",
            [NSRegularExpression escapedPatternForString:inputText]];
request.predicate = [NSPredicate predicateWithFormat:@"definitions MATCHES[cd] %@",
                                    searchTerm];

这篇关于NSPredicate使用核心数据,在字符串属性中搜索带有边界的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆