Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8100637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:47:00+00:00 2026-06-05T22:47:00+00:00

This must be a duplicate. But with so many NSPredicate questions out there, I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:47:02+00:00Added an answer on June 5, 2026 at 10:47 pm

    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];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This must be a FAQ, but I can’t find a duplicate question! There are
This must, must, must be a duplicate but I can't find the answer either
(I feel like this must be a duplicate, but I can't find it). Consider:
This must be simple, but I can't seem to figure it out. I am
This must be quite simple but I can't seem to figure this out. I've
OK, this must be a duplicate question, but I cannot find the answer: I
This must be a stupid question, but nevertheless I find it curious: Say I
this must be such a simple problem but can someone tell me why this
This must be very simple, but I couldn't find it on the documentation. In
This must be a very stupid question, but how does one recode with Xuggler?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.