Hi,

I have a piece of code, which gets a string of code from a webpage. It's the HTML source code, from which I want to make an array, in which I can find data the user will input. However, I need to extract all the useful information from the array, and discard the useless. How to search for a two substrings in a string, and copy the string in between?

My code:

NSString *googleString = @"http://www.mypage.com"; 
NSURL *googleURL = [NSURL URLWithString:googleString];
NSError *error;
NSString *googlePage = [NSString stringWithContentsOfURL:googleURL 
                                                encoding:NSASCIIStringEncoding
                                                   error:&error];

returns something like:

<HTML>
<BODY>
<TABLE style="border: Solid 1px Black; border-collapse: collapse; font-family: arial; width: 100%;">
<tr>  <td BGCOLOR="DCDCDC" NOWRAP style="border: Solid 1px Black; font-family: arial; padding: 2px; width: 100%;">
<A HREF ="1.htm" target="main">H4A</A>
  </td>
  <td BGCOLOR="DCDCDC" NOWRAP style="border: Solid 1px Black; font-family: arial; padding: 2px; width: 100%;">
<A HREF ="1.htm" target="main">Aanen </A>
  </td>
  <td BGCOLOR="DCDCDC" NOWRAP style="border: Solid 1px Black; font-family: arial; padding: 2px; width: 100%;">

<A HREF ="1.htm" target="main">Joeri</A>
  </td>
</tr>
<tr>  <td BGCOLOR="DCDCDC" NOWRAP style="border: Solid 1px Black; font-family: arial; padding: 2px; width: 100%;">
<A HREF ="2.htm" target="main">H4A</A>
  </td>
  <td BGCOLOR="DCDCDC" NOWRAP style="border: Solid 1px Black; font-family: arial; padding: 2px; width: 100%;">
<A HREF ="2.htm" target="main">Ali </A>
  </td>

  <td BGCOLOR="DCDCDC" NOWRAP style="border: Solid 1px Black; font-family: arial; padding: 2px; width: 100%;">
<A HREF ="2.htm" target="main">Sohail</A>
  </td>
</tr>
<tr>  <td BGCOLOR="DCDCDC" NOWRAP style="border: Solid 1px Black; font-family: arial; padding: 2px; width: 100%;">
<A HREF ="3.htm" target="main">H4A</A>
  </td>
  <td BGCOLOR="DCDCDC" NOWRAP style="border: Solid 1px Black; font-family: arial; padding: 2px; width: 100%;">
<A HREF ="3.htm" target="main">Beerthuijzen </A>

  </td>
  <td BGCOLOR="DCDCDC" NOWRAP style="border: Solid 1px Black; font-family: arial; padding: 2px; width: 100%;">
<A HREF ="3.htm" target="main">Iris</A>
  </td>
</tr>

and so on...

Maybe someone has a better idea? I need to get the strings: Name, Sirname, and page number (Example (The last one): Iris, Beerthuijzen, 3).

Thanks in advance!

Dani AI

Generated

Short answer: prefer a real HTML parser, but for consistently-structured HTML the fastest fix is to extract each <tr> and then the <a> anchors inside it. This builds on 's point about parsers while giving a concrete, workable approach for the sample HTML from .

A compact, practical regex-based example (Foundation only). It finds each table row, then all anchors in that row and reads href + inner text:

NSError *error = nil;
NSRegularExpression *rowRE = [NSRegularExpression regularExpressionWithPattern:@"<tr[^>]*>(.*?)</tr>"
                                                                       options:(NSRegularExpressionCaseInsensitive|NSRegularExpressionDotMatchesLineSeparators)
                                                                         error:&error];
NSRegularExpression *aRE = [NSRegularExpression regularExpressionWithPattern:@"<a[^>]*href\\s*=\\s*\"([^\"]+)\"[^>]*>([^<]+)</a>"
                                                                      options:NSRegularExpressionCaseInsensitive
                                                                        error:&error];

NSArray *rows = [rowRE matchesInString:html options:0 range:NSMakeRange(0, html.length)];
for (NSTextCheckingResult *rm in rows) {
    NSString *rowHTML = [html substringWithRange:[rm rangeAtIndex:1]];
    NSArray *anchors = [aRE matchesInString:rowHTML options:0 range:NSMakeRange(0, rowHTML.length)];
    if (anchors.count >= 3) {
        NSString *href = [rowHTML substringWithRange:[anchors[0] rangeAtIndex:1]];
        NSString *page  = [[[href lastPathComponent] stringByDeletingPathExtension] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSString *surname = [[rowHTML substringWithRange:[anchors[1] rangeAtIndex:2]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSString *givenName = [[rowHTML substringWithRange:[anchors[2] rangeAtIndex:2]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        // now have givenName, surname, page (e.g. "Iris", "Beerthuijzen", "3")
    }
}

Notes and cautions: regex is brittle on real-world HTML (nested tags, missing closing tags, inline attributes, varied case). For robust parsing use libxml2 or an XPath wrapper (TFHpple/Fuzi) to select rows and anchors, and to decode HTML entities. Also avoid synchronous network calls (the original used ASCII encoding and synchronous fetch); prefer NSURLSession and detect UTF-8/encoding. Trim whitespace and normalize strings before storing.

HTML's are supposed to follow strict xml syntax (not all web pages do so) But the html that your have posted does follow xml syntax so you can use xml parsers and extract data from this. If the page is not xml complaint then you might have to use reg-ex pattern match to extract data. I used this when I first started with reg-ex

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.