从 HTML 中检索内容,这是一个 NSString
Retrieve contents from HTML which is a NSString
这是我的 NSString :
NSString timeString = @"<h5 style="direction:ltr"><span data-version-created-date="20180326T120530.000+0000" class="releasedDate">26-Mar-2018 12:05:30</span></h5>";
我只想检索 span 标签中的“26-Mar-2018 12:05:30”。
我如何在 Objective C 中做到这一点?
请注意:给定的 HTML 是 NSString 格式。
试试这个
- (NSString *)stringByStrippingHTML : (NSString*) s {
NSRange r;
while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
s = [s stringByReplacingCharactersInRange:r withString:@""];
return s;
}
这将通过删除方括号 (<>) 的表达式来实现。已将斜杠 () 添加到 timeString 以使其成为正确的 NSString*。剥离重复四次,bt应该有条件循环。
NSString * timeString = @"<h5 style=\"direction:ltr\"><span data-version-created-date=\"20180326T120530.000+0000\" class=\"releasedDate\">26-Mar-2018 12:05:30</span></h5>";
NSRange openRange = [timeString rangeOfString:@"<"];
NSRange closeRange = [timeString rangeOfString:@">"];
NSRange enclosedRange = NSMakeRange(openRange.location, closeRange.location-openRange.location+1);
timeString = [timeString stringByReplacingCharactersInRange:enclosedRange withString:@""];
openRange = [timeString rangeOfString:@"<"];
closeRange = [timeString rangeOfString:@">"];
enclosedRange = NSMakeRange(openRange.location, closeRange.location-openRange.location+1);
timeString = [timeString stringByReplacingCharactersInRange:enclosedRange withString:@""];
openRange = [timeString rangeOfString:@"<"];
closeRange = [timeString rangeOfString:@">"];
enclosedRange = NSMakeRange(openRange.location, closeRange.location-openRange.location+1);
timeString = [timeString stringByReplacingCharactersInRange:enclosedRange withString:@""];
openRange = [timeString rangeOfString:@"<"];
closeRange = [timeString rangeOfString:@">"];
enclosedRange = NSMakeRange(openRange.location, closeRange.location-openRange.location+1);
timeString = [timeString stringByReplacingCharactersInRange:enclosedRange withString:@""];
NSLog(@"timeString = %@", timeString);
这对我有用
NSString *timeString = @"<h5 style=\"direction:ltr\"><span data-version-created-date=\"20180326T120530.000+0000\" class=\"releasedDate\">26-Mar-2018 12:05:30</span></h5>";
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@">\d.+\d<"
options:NSRegularExpressionCaseInsensitive
error:NULL];
[regex enumerateMatchesInString:timeString options:0 range:NSMakeRange(0, [timeString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
// your code to handle matches here
NSString *subString = [timeString substringWithRange:match.range];
NSLog(@"%@",[subString substringWithRange:NSMakeRange(1, subString.length - 2)]);
}];
如果您想确保获得 span 标签之间的日期,最好比剥离所有 HTML 标签并假设唯一剩下的就是日期更明确,或者假设整个 HTML 文本中只有一个 span 标签。它现在可能有效,但如果 HTML 发生变化,将来可能会中断。
NSString * timeString = @"<h5 style=\"direction:ltr\"><span data-version-created-date=\"20180326T120530.000+0000\" class=\"releasedDate\">26-Mar-2018 12:05:30</span><span class=\"someOtherClass\">garbageData</span></h5>";
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@"<span.*class=\"releasedDate\"[^>]*>(.*)</span.*>"
options:NSRegularExpressionCaseInsensitive
error:nil];
NSTextCheckingResult *textCheckingResult = [regex firstMatchInString:timeString options:0 range:NSMakeRange(0, timeString.length)];
NSString *releaseDateString = [timeString substringWithRange:[textCheckingResult rangeAtIndex:1]];
if( ! [releaseDateString isEqualToString:@""] )
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MMM-yyyy' 'HH:mm:ss"];
NSDate *releaseDate = [dateFormatter dateFromString:releaseDateString];
NSLog( @"%@ - %@", releaseDateString, releaseDate );
}
请注意,即使 HTML 文本中有其他跨度,这仍然有效。它专门拉出带有 class "releasedDate".
的那个
这是我的 NSString :
NSString timeString = @"<h5 style="direction:ltr"><span data-version-created-date="20180326T120530.000+0000" class="releasedDate">26-Mar-2018 12:05:30</span></h5>";
我只想检索 span 标签中的“26-Mar-2018 12:05:30”。 我如何在 Objective C 中做到这一点?
请注意:给定的 HTML 是 NSString 格式。
试试这个
- (NSString *)stringByStrippingHTML : (NSString*) s {
NSRange r;
while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
s = [s stringByReplacingCharactersInRange:r withString:@""];
return s;
}
这将通过删除方括号 (<>) 的表达式来实现。已将斜杠 () 添加到 timeString 以使其成为正确的 NSString*。剥离重复四次,bt应该有条件循环。
NSString * timeString = @"<h5 style=\"direction:ltr\"><span data-version-created-date=\"20180326T120530.000+0000\" class=\"releasedDate\">26-Mar-2018 12:05:30</span></h5>";
NSRange openRange = [timeString rangeOfString:@"<"];
NSRange closeRange = [timeString rangeOfString:@">"];
NSRange enclosedRange = NSMakeRange(openRange.location, closeRange.location-openRange.location+1);
timeString = [timeString stringByReplacingCharactersInRange:enclosedRange withString:@""];
openRange = [timeString rangeOfString:@"<"];
closeRange = [timeString rangeOfString:@">"];
enclosedRange = NSMakeRange(openRange.location, closeRange.location-openRange.location+1);
timeString = [timeString stringByReplacingCharactersInRange:enclosedRange withString:@""];
openRange = [timeString rangeOfString:@"<"];
closeRange = [timeString rangeOfString:@">"];
enclosedRange = NSMakeRange(openRange.location, closeRange.location-openRange.location+1);
timeString = [timeString stringByReplacingCharactersInRange:enclosedRange withString:@""];
openRange = [timeString rangeOfString:@"<"];
closeRange = [timeString rangeOfString:@">"];
enclosedRange = NSMakeRange(openRange.location, closeRange.location-openRange.location+1);
timeString = [timeString stringByReplacingCharactersInRange:enclosedRange withString:@""];
NSLog(@"timeString = %@", timeString);
这对我有用
NSString *timeString = @"<h5 style=\"direction:ltr\"><span data-version-created-date=\"20180326T120530.000+0000\" class=\"releasedDate\">26-Mar-2018 12:05:30</span></h5>";
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@">\d.+\d<"
options:NSRegularExpressionCaseInsensitive
error:NULL];
[regex enumerateMatchesInString:timeString options:0 range:NSMakeRange(0, [timeString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
// your code to handle matches here
NSString *subString = [timeString substringWithRange:match.range];
NSLog(@"%@",[subString substringWithRange:NSMakeRange(1, subString.length - 2)]);
}];
如果您想确保获得 span 标签之间的日期,最好比剥离所有 HTML 标签并假设唯一剩下的就是日期更明确,或者假设整个 HTML 文本中只有一个 span 标签。它现在可能有效,但如果 HTML 发生变化,将来可能会中断。
NSString * timeString = @"<h5 style=\"direction:ltr\"><span data-version-created-date=\"20180326T120530.000+0000\" class=\"releasedDate\">26-Mar-2018 12:05:30</span><span class=\"someOtherClass\">garbageData</span></h5>";
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@"<span.*class=\"releasedDate\"[^>]*>(.*)</span.*>"
options:NSRegularExpressionCaseInsensitive
error:nil];
NSTextCheckingResult *textCheckingResult = [regex firstMatchInString:timeString options:0 range:NSMakeRange(0, timeString.length)];
NSString *releaseDateString = [timeString substringWithRange:[textCheckingResult rangeAtIndex:1]];
if( ! [releaseDateString isEqualToString:@""] )
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MMM-yyyy' 'HH:mm:ss"];
NSDate *releaseDate = [dateFormatter dateFromString:releaseDateString];
NSLog( @"%@ - %@", releaseDateString, releaseDate );
}
请注意,即使 HTML 文本中有其他跨度,这仍然有效。它专门拉出带有 class "releasedDate".
的那个