1

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?

zaph
  • 111,848
  • 21
  • 189
  • 228
Harrienak
  • 67
  • 9
  • Seems iOS, not IOS... –  Nov 18 '13 at 23:10
  • 1
    Have you looked at the docs for `NSString`? Have you done any searching? There are many existing topics on extracting a substring from a string. – rmaddy Nov 18 '13 at 23:19
  • 1
    Also, where does the string come from? If it's arbitrary HTML, you'd better use a XML parser. I'll just drop this http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 here, before someone suggests to use a regex. – Gabriele Petronella Nov 18 '13 at 23:23

2 Answers2

0

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'

zaph
  • 111,848
  • 21
  • 189
  • 228
  • 1
    Keep in mind that most HTML is not valid XML. It depends on the HTML in question. This means that standard XML parsers will choke on general HMTL. – rmaddy Nov 19 '13 at 00:07
  • @rmaddy if you are using the type of HTML that you shan't use (i. e. HTML != 5). HTML5, then only format one must ever consider using, is valid XML. –  Nov 20 '13 at 22:26
-2

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.

SPA
  • 1,279
  • 8
  • 13
  • 1
    it's like going by car to the bus station to buy a ticket, then going back home with a bus. –  Nov 18 '13 at 23:41
  • 1
    Why going through the hassle of putting the HTML in a webview and extracting it again, when you already have it? If it's arbitrary HTML a simple XML parser should do, but it's impossible to say unless the OP provides more details. – Gabriele Petronella Nov 18 '13 at 23:45
  • the html isn't being extracted again, the innerHTML is, i.e. the stuff inside the tag, as requested by the OP. An XML parser would take more than 3 lines, so how is it complicated? – SPA Nov 18 '13 at 23:51
  • 1
    Ok, maybe complicated is not the right adjective. Let's say it's an overkill. Using an XML would easily be a one-liner and would avoid spawning a totally unnecessary full-fledged `UIWebView` – Gabriele Petronella Nov 18 '13 at 23:56
  • I've edited my answer to make it easier to understand for those unfamiliar with stringByEvaluatingJavaScriptFromString – SPA Nov 19 '13 at 00:13