I got a NSString like:
NSString *s = @"<span class='class1'>here some text</span>";
class1 can be anything (like class2, or largetext, or whatever).
I want to get the following:
NSString *wantedString = @"here some text";
How can i do that?
I got a NSString like:
NSString *s = @"<span class='class1'>here some text</span>";
class1 can be anything (like class2, or largetext, or whatever).
I want to get the following:
NSString *wantedString = @"here some text";
How can i do that?
Using an xml parser is a great solution provided the overhead is worth it.
Using a regular expression:
NSString *s = @"<span class='class1'>here some text</span>";
NSString *pattern = @"<span class='[^']+'>([^<]+)";
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:pattern
options:NSRegularExpressionCaseInsensitive
error:nil];
NSTextCheckingResult *textCheckingResult = [regex firstMatchInString:s options:0 range:NSMakeRange(0, s.length)];
NSRange matchRange = [textCheckingResult rangeAtIndex:1];
NSString *match = [s substringWithRange:matchRange];
NSLog(@"Found string '%@'", match);
NSLog output:
Found string 'here some text'
If this html is already loaded into a web view, use stringByEvaluatingJavaScriptFromString to access the innerHTML of the span tag.
stringByEvaluatingJavaScriptFromString can be used to access any part of html through the DOM (Document Object Model), such as the content of a tag.