Building an iOS app that needs to display some HTML string in a UIWebView object. I am trying to search, find for a pattern and replace with the proper link to an image. The image links are original something like [pic:brand:123], where pic is always pic , brand can be any alphanumeric, and the 123 is can also be any non-whitespace alphanumeric.
So far I have tried a few including:
NSString *pattern = @"\\[pic:([^\\s:\\]]+):([^\\]])\\]";
But none has worked for so far.
Here is a sample code:
NSString *str = @"veryLongHTMLSTRING";
NSLog(@"Original test: %@",[str substringToIndex:500]);
NSError *error = nil;
// use regular expression to replace the emoji
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@"\\[pic:([^\\s:\\]]+):([^\\]])\\]"
options:NSRegularExpressionCaseInsensitive error:&error];
if(error != nil){
NSLog(@"ERror: %@",error);
}else{
[regex stringByReplacingMatchesInString:str
options:0
range:NSMakeRange(0, [str length])
withTemplate:[NSString stringWithFormat:@"/%@/photo/%@.gif",
IMAGE_BASE_URL, @"$1/$2"]];
NSLog(@"Replaced test: %@",[str substringToIndex:500]);
[pic:brand:123]to be/photo/brand/123.gif. brand is generic here though. It could between anything between numerous options.