iphone - Matching string format -


how possible perform matching of string, regex?

for example have array of strings, want match if these string in format of "abc def xxx", xxx number 1, 11, 12, 100, 111, etc.

how can achieve this?

the arrays:

nsarray *array1 = [nsarray arraywithobjects:@"abc def 1", @"abc def 64", @"abc def 853", @"abc def 14", nil]; nsarray *array2 = [nsarray arraywithobjects:@"abc def 3", @"abc def 856", @"abc def 36", @"abc def 5367", nil]; 

the regular expression:

nsstring *regex = @"abc def [0-9]{1,3}"; 

to check if strings in array matche regex:

nspredicate *predicate = [nspredicate predicatewithformat:@"all description matches %@", regex]; bool allstringsmatch = [predicate evaluatewithobject:array1];  // output: yes allstringsmatch = [predicate evaluatewithobject:array2]; // output: no 

all objects in array1 matches regex, array2 contains string @"abc def 5367" doesn't match regex.

to matched strings:

nspredicate *predicate = [nspredicate predicatewithformat:@"description matches %@", regex]; nsarray *unmatchedstrings = [array2 filteredarrayusingpredicate:predicate]; // output: { @"abc def 3", @"abc def 856", @"abc def 36" } 

to unmatched strings:

nspredicate *predicate = [nspredicate predicatewithformat:@"not description matches %@", regex]; nsarray *unmatchedstrings = [array2 filteredarrayusingpredicate:predicate]; // output: { @"abc def 5367" } 

note here "description" in predicate - description method of nsstring.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -