1

Given a string attribute like this "Red Globe 21# Bag SO2 SF White 5L Styro A"

I need to match By "Red Globe 21#" (or|and) "Bag" (or|and) "SO2" (or|and) "SF White".

Following the sample posted by dasblinkenlight NSPredicate Exact Match with String

I was trying to figure out the right combination for this RegEx

NSString *str = @"Globe";
    NSMutableString *arg = [NSMutableString string];
    [arg appendString:@"\\s*\\b"];
    [arg appendString:str];
    [arg appendString:@"\\b\\s*\\w*"];
    NSPredicate *p = [NSPredicate predicateWithFormat:@"SELF matches[c] %@", arg];
    NSArray *a = [NSArray arrayWithObjects:@"Red Globe 21# Bag SO2 SF White 5L Styro A ", @"test", @"Test", @"TEST", nil];
    NSArray *b = [a filteredArrayUsingPredicate:p];

    NSLog(@"Result ;%@",b);`

But It does no work for me, I will appreciate your help.

Community
  • 1
  • 1
Jose
  • 1,027
  • 1
  • 14
  • 23
  • What does that `(or|and)` mean? What do you want the matched group(s) to be for that sample string? Can you give us another example that's not a perfect match, and tell us what you want for that one? – abarnert Aug 13 '14 at 00:00
  • For example on this other string "Flame 19# Pouch SO2 J.A.P. White 5L Styro A" I need when a match for "Flame 19#" should return all "Flame 19# .? records, but if is given "Flame 19#" and "Pouch", should get all records that contain that combination, narrowring the result. Regarding the Or | And I meant that any of the segments can be given alone or combined. Thanks – Jose Aug 13 '14 at 00:28

1 Answers1

1

If I understand what you mean, I'm not sure what you think regexps can do for you in the first place.

You have a handful of different exact strings. You want to be able to pick a subset of those strings, and match every element that has contains of the strings in that subset, right?

So, you just need to AND together exact substring matches: "SELF contains %@ && SELF contains %@".

abarnert
  • 354,177
  • 51
  • 601
  • 671