-1

I am attempting to get my school's notification shown on app. Here is the source code of the HTML of notification.

<div id="tab1_bottom" key="l7ob6tgg-0oe4-hbkw-7gdr-0vddvv41qbsd">
    <div class="tab1_bottom1" title="上海廿一客食品2018校园宣讲会">上海廿一客食品2018校园宣讲会</div>
    <div class="tab1_bottom2" title="专场宣讲会">专场宣讲会</div>
    <div class="tab1_bottom3">2017-11-13</div>
    <div class="tab1_bottom4">15:30-17:30</div>
    <div class="tab1_bottom5" title="叶耀珍楼202">叶耀珍楼202</div>
</div>

My action: Use NSURLSessionDataTask to get HTML, and saved as htmlstring. Then, I use NSRegularExpression+"(AA)(.*?)(BB)" to extract the htmlstring for the information I want.

    NSString *htmlstring = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(1\" title=\")(.*?)(\">)" options:0 error:nil];
    NSArray *matches = [regex matchesInString:htmlstring options:0 range:NSMakeRange(0,htmlstring.length)];

    for(NSTextCheckingResult *result in [matches objectEnumerator]) {
            NSRange matchRange1 = [result range];
            NSString *newStr1=[htmlstring substringWithRange:matchRange1];
            NSLog(@"%@",newStr1);
    }

Q1:what finally I got were "(AA)(.?)(BB)", but I just want "(.?)", not AABB. How to fix it ?

Q2:I hope to save newStr1 as a Array and write it on UILabel. However, what I now got was a String, not a Array. I cannot write it on my TableView Label.

What I want shown on my table is just like this https://github.com/AlexLLL/MyFudanCampus/blob/master/MyFudanCampus/screenshots/Simulator%20Screen%20Shot%20-%20iPhone%208%20Plus%20-%202017-11-12%20at%2022.07.57.png

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Alex Li
  • 55
  • 1
  • 9
  • Possible duplicate of [Capture groups not working in NSRegularExpression](https://stackoverflow.com/questions/6822356/capture-groups-not-working-in-nsregularexpression) – Wiktor Stribiżew Nov 12 '17 at 15:04

2 Answers2

0

This might solve your problem.

-(NSString *)convertHTML:(NSString *)html {

    NSScanner *myScanner;
    NSString *text = nil;
    myScanner = [NSScanner scannerWithString:html];

    while ([myScanner isAtEnd] == NO) {

        [myScanner scanUpToString:@"<" intoString:NULL] ;

        [myScanner scanUpToString:@">" intoString:&text] ;

        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@""];
    }
    //
    html = [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    return html;
}

Refrenced from this link

Sucharu Hasija
  • 1,096
  • 11
  • 23
0

In your loop you are using:

NSRange matchRange1 = [result range];

The range method returns the range of the complete match. To obtain the range of an individual capture group use rangeAtIndex: passing the capture group number. Your RE has three capture groups and you want the second so pass 2.

To store your results in an array simply create a new mutable array before your loop, something like:

NSMutableARray *matchedStrings = [NSMutableArray new];

And then in your loop use addObject: to add each substring to the array. The final loop body becomes something like:

NSRange matchRange1 = [result rangeAtIndex:2];
[matchedStrings addObject:[htmlstring substringWithRange:matchRange1]];

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
  • Thanks. In the end, I give up and choose wkwebview to establish my HTML view page. It was not cool but works....... – Alex Li Nov 13 '17 at 16:46