The original address of this article http://www.cnblogs.com/qianLL/p/6082287.html
Sometimes back in the background is a string of HTML ' strings we need to extract the image address inside the key is a correct regular expression
That
< (img| IMG) (. *?) (/>|></img>|>)
The code below returns all the image addresses in this string, all of which are a collection.
+ (Nsarray *) Filterimage: (NSString *) html{Nsmutablearray*resultarray =[Nsmutablearray array]; Nsregularexpression*regex = [Nsregularexpression regularexpressionwithpattern:@"< (img| IMG) (. *?) (/>|></img>|>)"Options:nsregularexpressionallowcommentsandwhitespace Error:nil]; Nsarray*result = [Regex matchesinstring:html options:nsmatchingreportcompletion Range:nsmakerange (0, Html.length)]; for(Nstextcheckingresult *iteminchresult) {NSString*imghtml = [HTML Substringwithrange:[item rangeatindex:0]]; Nsarray*tmparray =Nil; if([imghtml rangeofstring:@"src=\ ""].location! = nsnotfound) {Tmparray = [imghtml componentsseparatedbystring:@"Src=\""]; } Else if([imghtml rangeofstring:@"src="].location! =nsnotfound) {Tmparray= [imghtml componentsseparatedbystring:@"src="]; } if(Tmparray.count >=2) {NSString*SRC = tmparray[1]; Nsuinteger Loc= [src rangeofstring:@"\ ""].location; if (loc! = nsnotfound) {src = [src substringtoindex:loc]; [Resultarray ADDOBJECT:SRC]; }}} return resultarray;}
iOS extracts the address of an IMG in an HTML string (picture address)