NSPredicate带有与单词开头匹配的字符串 [英] NSPredicate with a string matching beginning of words

查看:141
本文介绍了NSPredicate带有与单词开头匹配的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这必须是重复的。但是有那么多NSPredicate问题,我找不到正确的问题。



我有一个包含<$的Core Data对象列表。 c $ c> compositeName 字段。例如艾米莉·布朗特(Emily Blunt)的名字可能就在其中。我想使用 NSPredicate 搜索列表,这将使我既搜索 Em又搜索 Bl,然后使该名称显示在获取的结果中。 / p>

这必须超级简单,但正如您所猜,我没有看到它。我对带有正则表达式的NSPredicate的功能失常尝试如下:

  [NSPredicate predicateWithFormat:@ compositeName MATCHES [cd]' 。*(?< = ^ | \\s)%@。*',查询]; 

我对这个正则表达式的想法是:




  • 在空格之前或后面开始的任何数量的字符

  • 负向后看

  • 查询




之后的任意数量的字符,但是它不起作用。我没有任何结果。我该如何解决?



P.S。如果有一个不带正则表达式的NSPredicate解决方案也适合我的目的。

解决方案

您需要在外部构建整个正则表达式模式字符串谓词字符串,然后将其传递给谓词。



此外,还有一个正则表达式构造 \b 匹配单词边界,这就是您所追求的。



因此,此摘要应为您提供一个简单的解决方案:

  NSString * searchString = @ Em; 
NSString * regexString = [NSString stringWithFormat:@。* \\b%@。*,searchString];

NSPredicate * pred = [NSPredicate predicateWithFormat:@ self.compositeName match [cd]%@,regexString];

如果未清除输入的搜索文本,则可能需要先进行此操作



另一种改进是可以在单词边界处接受unicode字符,以便人们搜索Ántonio,Über和其他类似的字符串。



以下代码行将创建一个正则表达式模式,该模式将同时考虑这两个方面:

  NSString * orgSearchString = @ ^; 

NSString * escapedSearchString = [NSRegularExpression escapedPatternForString:orgSearchString];

NSString * regexString = [NSString stringWithFormat:@(?w:)。* \\b%@。*,escapedSearchString];


This must be a duplicate. But with so many NSPredicate questions out there, I can't find the right one.

I have a list of Core Data objects that contain a compositeName field. A name like 'Emily Blunt' could be in there. I want to search the list using an NSPredicate that will let me search for "Em" but also for "Bl" and then have this name show up in the fetched results.

This must be super easy, but as you'd guess, I'm not seeing it. My dysfunctional attempt at an NSPredicate with a regular expression looks like this:

[NSPredicate predicateWithFormat:@"compositeName MATCHES[cd] '.*(?<=^|\\s)%@.*'", query];

My thought for this regular expression were:

  • any number of characters before
  • negative lookbehind spaces or beginning
  • the query
  • any number of characters after

But it's not working. I'm not getting any results. How do I fix this?

P.S. If there's an NSPredicate solution without Regular Expressions that would suit my purpose too.

解决方案

You need to build the whole regex pattern string outside of the predicate string, then pass it to the predicate.

Also, there is a regex construct \b to match a word boundary, which is what you are after.

So altogether, this snippet should give you a simple solution to your problem:

NSString *searchString = @"Em";
NSString *regexString  = [NSString stringWithFormat:@".*\\b%@.*", searchString];

NSPredicate *pred = [NSPredicate predicateWithFormat:@"self.compositeName matches[cd] %@", regexString];

If the input search text has not been cleaned, you might want to do that before you pass it on to the regex engine.

Another improvement could be to accept unicode characters at the word boundary to allow people to search for 'Ántonio', 'Über' and other similar strings.

The following lines of code will create a regex pattern that will take care of both aspects:

NSString *orgSearchString = @"^";

NSString *escapedSearchString = [NSRegularExpression escapedPatternForString: orgSearchString];

NSString *regexString  = [NSString stringWithFormat:@"(?w:).*\\b%@.*", escapedSearchString];

这篇关于NSPredicate带有与单词开头匹配的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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