我需要一些帮助来构建一个正则表达式,以从一个长字符串中删除带有搜索词的 href 链接,然后将其解析为 Web View
href 字符串的示例:<a href="/search/?search=Huntington">Huntington</a>
我想删除链接的纯文本以外的所有内容(只是链接本身)但是遇到了麻烦
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<a href=\"/search/?search=([A-Z][a-z])\"" options:NSRegularExpressionCaseInsensitive error:&error];
非常欢迎任何帮助
谢谢
最佳答案
我觉得
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"];
应该可以工作(我在 TextMate 中测试了正则表达式,但在 XCode 中没有)。
关于ios - ios中的正则表达式删除href链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11665109/