Vahid
Vahid

Reputation: 3496

How to make `NSPredicate` to ignore a some characters in searching

for exam:

I stored V«003» text in the core data id attribute.

If I pass the 003 or V003 to the NSPredicate it should approve the search, but because of « and » characters the NSFetchRequest couldn't find the field.

How could I ignore « and » characters in NSPredicate?

Is there any Regular Expression way for NSPredicate?

Upvotes: 0

Views: 572

Answers (1)

vadian
vadian

Reputation: 285180

It's pretty easy with Regular Expression, the question mark indicates an optional character.

let pattern = "V«?003»?"
NSPredicate(format: "id MATCHES %@", pattern)

Edit: to fix user input, I added «?»? between every user input characters:

func generatePattern(item: String) -> String {
    var str = ""
    for st in item {
        str.append(contentsOf: "«?»?")
        str.append(st)
    }
    str.append(contentsOf: "«?»?")
    return String(str)
}

Upvotes: 1

Related Questions