7

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

Alan Moore
  • 73,866
  • 12
  • 100
  • 156

2 Answers2

10

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).

Zoleas
  • 4,869
  • 1
  • 21
  • 33
3

@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;
}
Erhan Demirci
  • 4,173
  • 4
  • 36
  • 44