【发布时间】:2014-03-12 21:35:19
【问题描述】:
我想在Objective C中写一个url提取函数。输入文本可以是任何东西,可能包含也可能不包含html锚标签。
考虑一下:
NSString* input1 = @"This is cool site <a href="https://abc.com/coolstuff"> Have fun exploring </a>";
NSString* input2 = @"This is cool site <a target="_blank" href="https://abc.com/coolstuff"> Must visit </a>";
NSString* input3 = @"This is cool site <a href="https://abc.com/coolstuff" target="_blank" > Try now </a>";
我想修改字符串为"This is cool site https://abc.com/coolstuff
忽略锚标记之间的所有文本。并且需要考虑锚标签中的_target等其他属性
我可以做类似的事情
static NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<a\shref=\"(.*?)\">.*?</a>" options:NSRegularExpressionCaseInsensitive error:nil];;
NSString* modifiedString = [regex stringByReplacingMatchesInString:inputString options:0 range:NSMakeRange(0, [inputString length]) withTemplate:@"$1"];
使用 input1 可以正常工作,但在其他情况下会失败。
谢谢
【问题讨论】:
标签: html ios regex nsregularexpression