iphone - NSPredicates beginsWith -


i have nsarray contents strings format similar to: [a-z]{+}-[0-9]{+}

so bunch of repeating alpha characters, separator, , 1 or more digits

i want filter values in array match separator can't seem explicitly specify in predicator's format:

nspredicate *apredicate = [nspredicate predicatewithformat:@"self beginswith %@", avalue]; nsarray *filtered = [entries filteredarrayusingpredicate:apredicate]; 

how constrain filtering such case?

you use "matches" operator regular expression search, so:

nspredicate * p = [nspredicate predicatewithformat:@"self matches %@", @"[a-z]+-.*"]; nsarray * s = [nsarray arraywithobject:@"abc-123"]; nslog(@"%@", [s filteredarrayusingpredicate:p]); 

there caveat, though. regular expression matched across entire string. if want find elements begin 3 letters, expression can't "[a-z]{3}". has "[a-z]{3}.*". first fail that's not 3 letters, whereas second match that's @ least 3 letters long.

took me while realize this...


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -