ios - NsRegularExpressions several float numbers separate with commas -
i try evaluate sentence:
<font color="0.839216,0.839216,0.839216" strokecolor="none" size="35" face="avenir-book"> i need extract 3 float values of color attribute
with code:
nsregularexpression* colorregex = [[[nsregularexpression alloc] initwithpattern:@"color=([0-9]+),([0-9]+),([0-9]+)" options:0 error:null] autorelease]; [colorregex enumeratematchesinstring:tag options:0 range:nsmakerange(0, [tag length]) usingblock:^(nstextcheckingresult *match, nsmatchingflags flags, bool *stop){ nsnumber *redcomponent=[nsnumber numberwithfloat: [[tag substringwithrange: match.range] floatvalue] ]; nsnumber *greencomponent=[nsnumber numberwithfloat: [[tag substringwithrange: match.range ] floatvalue] ]; nsnumber *bluecomponent=[nsnumber numberwithfloat: [[tag substringwithrange: match.range] floatvalue] ]; self.color=[[uicolor alloc] initwithred:redcomponent.floatvalue green:greencomponent.floatvalue blue:bluecomponent.floatvalue alpha:1]; nslog(@"color %@,%@,%@",redcomponent,greencomponent,bluecomponent); i tested several regular expressions did not succeed, me ?
your pattern not match quotes " , dots ., should like:
initwithpattern:@"color=\"([0-9.]+),([0-9.]+),([0-9.]+)\"" and inside block, have use rangeatindex: values of capture group, (simplifying code bit):
float redcomponent = [[tag substringwithrange: [match rangeatindex:1]] floatvalue]; float greencomponent = [[tag substringwithrange: [match rangeatindex:2]] floatvalue]; float bluecomponent = [[tag substringwithrange: [match rangeatindex:3]] floatvalue]; self.color = [[uicolor alloc] initwithred:redcomponent green:greencomponent blue:bluecomponent alpha:1]; nslog(@"color %f,%f,%f", redcomponent, greencomponent, bluecomponent);
Comments
Post a Comment