Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression in ios to remove href links

i need some help in building a regular expression to remove href links with search terms from a long string that i then parse into a web view

an example of the href string : <a href="/search/?search=Huntington">Huntington</a>

i would like to remove everthing but the plain text of the link (just the link itself) but having troubles

 NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<a href=\"/search/?search=([A-Z][a-z])\"" options:NSRegularExpressionCaseInsensitive error:&error];

any help would be greatly welcomed

Thanks

like image 359
richard Stephenson Avatar asked Jul 26 '12 08:07

richard Stephenson


2 Answers

I think

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<a href=\"[^\"]+\">([^<]+)</a>" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@"$1"];

should work (I tested the regexp in TextMate but not in XCode).

like image 174
Zoleas Avatar answered Oct 04 '22 03:10

Zoleas


@Helium3 and @Carl Explain right above and I want to write as corectly and I created this function for delete a href tag from NSString

-(NSString *)deleteAHref:(NSString *)originalString
{
    NSError *regexError = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<a href=.*?>(.*?)</a>" options:NSRegularExpressionCaseInsensitive error:&regexError];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:originalString options:0 range:NSMakeRange(0, [originalString length]) withTemplate:@"$1"];
    return modifiedString;
}
like image 21
Erhan Demirci Avatar answered Oct 04 '22 04:10

Erhan Demirci