url IOS中的NSRegularExpression
我有一个从url获取标题的方法 它的工作,但在一个网站上,我没有比赛的结果 你知道问题出在哪里吗 网页上的标题是Test-sdsurl IOS中的NSRegularExpression,ios,regex,nsexpression,Ios,Regex,Nsexpression,我有一个从url获取标题的方法 它的工作,但在一个网站上,我没有比赛的结果 你知道问题出在哪里吗 网页上的标题是Test-sds NSURL *url_s = [NSURL URLWithString:url]; NSData* data = [NSData dataWithContentsOfURL:url_s]; if(data!=nil){ NSString* newStr = [NSString strin
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]];
}
NSURL*url_s=[NSURL-URLWithString:url];
NSData*data=[NSData data with contentsofURL:url\s];
如果(数据!=nil){
NSString*newStr=[NSString stringWithUTF8String:[数据字节]];
NSRegularExpression*regex=[NSRegularExpression regular expressionwithpattern:@“(.*)”选项:0错误:NULL];
NSTextCheckingResult*match=[regex firstMatchInString:newStr选项:0范围:NSMakeRange(0[newStr长度])];
NSString*title=[newStr substringWithRange:[match rangeAtIndex:1]];
}
根据模式匹配HTML时,应使用nsregularexpressioncasesincensitive
和nsregularexpressiondotmatcheslineseparator
选项
NSRegularExpressionOptions opts = NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<title>(.*)</title>"
options:opts
error:NULL];
NSRegularExpressionOptions opts=nsregularexpressioncase不敏感| nsregularexpressiondotmatcheslineseparator;
NSRegularExpression*regex=[NSRegularExpression regular expression with pattern:@”(.*)”
选项:选择
错误:NULL];
根据模式匹配HTML时,应使用nsregularexpressioncasesincensitive
和nsregularexpressiondotmatcheslineseparator
选项
NSRegularExpressionOptions opts = NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<title>(.*)</title>"
options:opts
error:NULL];
NSRegularExpressionOptions opts=nsregularexpressioncase不敏感| nsregularexpressiondotmatcheslineseparator;
NSRegularExpression*regex=[NSRegularExpression regular expression with pattern:@”(.*)”
选项:选择
错误:NULL];
根据模式匹配HTML时,应使用nsregularexpressioncasesincensitive
和nsregularexpressiondotmatcheslineseparator
选项
NSRegularExpressionOptions opts = NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<title>(.*)</title>"
options:opts
error:NULL];
NSRegularExpressionOptions opts=nsregularexpressioncase不敏感| nsregularexpressiondotmatcheslineseparator;
NSRegularExpression*regex=[NSRegularExpression regular expression with pattern:@”(.*)”
选项:选择
错误:NULL];
根据模式匹配HTML时,应使用nsregularexpressioncasesincensitive
和nsregularexpressiondotmatcheslineseparator
选项
NSRegularExpressionOptions opts = NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<title>(.*)</title>"
options:opts
error:NULL];
NSRegularExpressionOptions opts=nsregularexpressioncase不敏感| nsregularexpressiondotmatcheslineseparator;
NSRegularExpression*regex=[NSRegularExpression regular expression with pattern:@”(.*)”
选项:选择
错误:NULL];
您无法安全地从带有正则表达式的HTML或XML派生内容。XML和HTML是有状态的,因此它们实际上必须被解析为有状态的。例如,使用正则表达式将返回以下错误结果:
<html>
<head>
<!--<title>Old Title</title>-->
<title>New Title</title>
</head>
</html>
新标题
您应该选择HTML解析器并使用它。我以前在应用程序中成功使用过。您无法安全地从HTML或带有正则表达式的XML中派生内容。XML和HTML是有状态的,因此它们实际上必须被解析为有状态的。例如,使用正则表达式将返回以下错误结果:
<html>
<head>
<!--<title>Old Title</title>-->
<title>New Title</title>
</head>
</html>
新标题
您应该选择HTML解析器并使用它。我以前在应用程序中成功使用过。您无法安全地从HTML或带有正则表达式的XML中派生内容。XML和HTML是有状态的,因此它们实际上必须被解析为有状态的。例如,使用正则表达式将返回以下错误结果:
<html>
<head>
<!--<title>Old Title</title>-->
<title>New Title</title>
</head>
</html>
新标题
您应该选择HTML解析器并使用它。我以前在应用程序中成功使用过。您无法安全地从HTML或带有正则表达式的XML中派生内容。XML和HTML是有状态的,因此它们实际上必须被解析为有状态的。例如,使用正则表达式将返回以下错误结果:
<html>
<head>
<!--<title>Old Title</title>-->
<title>New Title</title>
</head>
</html>
新标题
您应该选择HTML解析器并使用它。我以前成功地在应用程序中使用过。我不知道问题出在哪里,但我看到一些HTML,人们会将字母大写。。。所以有可能有人把。。。。。这不会在常规表达式中返回结果,而不是将
0
传递到regularExpressionWithPattern:options:error:
中的options
参数,使用nsregularexpressionCaseSensitive
。另外,newStr
可以使用[nsstringwithcontentsofURL:encoding:error:
分配。不需要将HTML读入NSData,然后转换成NSString。嗨,我发现了问题所在,但我需要好的解决方案。在这个标题是新行…我不知道问题是什么,但我看到一些HTML,人们将大写字母。。。所以有可能有人把。。。。。这不会在常规表达式中返回结果,而不是将0
传递到regularExpressionWithPattern:options:error:
中的options
参数,使用nsregularexpressionCaseSensitive
。另外,newStr
可以使用[nsstringwithcontentsofURL:encoding:error:
分配。不需要将HTML读入NSData,然后转换成NSString。嗨,我发现了问题所在,但我需要好的解决方案。在这个标题是新行…我不知道问题是什么,但我看到一些HTML,人们将大写字母。。。所以有可能有人把。。。。。这不会在常规表达式中返回结果i