我有从 url 获取标题的方法。
它有效,但在一个网站上我没有匹配结果。
你知道问题出在哪里吗?
网页上的标题是Test - sds
NSURL *url_s = [NSURL URLWithString:url];
NSData* data = [NSData dataWithContentsOfURL:url_s];
if(data!=nil){
NSString* newStr = [NSString stringWithUTF8String:[data bytes]];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<title>(.*)</title>" options:0 error:NULL];
NSTextCheckingResult *match = [regex firstMatchInString:newStr options:0 range:NSMakeRange(0, [newStr length])];
NSString *title = [newStr substringWithRange:[match rangeAtIndex:1]];
}
最佳答案
在将 HTML 与模式匹配时,您应该使用 NSRegularExpressionCaseInsensitive
和 NSRegularExpressionDotMatchesLineSeparators
选项。
NSRegularExpressionOptions opts = NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<title>(.*)</title>"
options:opts
error:NULL];
关于ios - URL IOS 中的 NSRegularExpression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19450176/