NSRegularExpression for stripping HTML Tag

I am developing a ebook reader app. I have the .ePUB file for the entire ebook where in each topic of the ebook is a html file. I want to implement the search functionality in the app. I am using NSRegularExpression class for searching. Please consider the following html code:

<temp> I am temp in tempo with temptation </temp>

Say for example in the above html code I just want to search for the word temp. Now in above code temp is appearing 5 times -> <temp> </temp> temp tempo temptation. I am looking for a regular expression where I can only extract the whole word “temp”. I don’t want to consider the word temp in the html tags <temp> </temp>. I also don’t want the word tempo and temptation to be considered.

  • What are the correct regular expressions for syntax highlighting?
  • Check if a string contains at least a upperCase letter, a digit, or a speical character in Swift?
  • Find and Replace using patterns in Xcode 8
  • Objective-C/Swift regex matching across multliple lines
  • matchesInString function returning an empty Array in Swift
  • regex to extract all the substrings between two charachters or tags
  • Thanks in advance

    2 Solutions Collect From Internet About “NSRegularExpression for stripping HTML Tag”

    How is this?

    [^<\/?\w*>]+(temp\s)
    

    http://rubular.com/r/3PkdvNZSbr

    NSString *evaluate_string = @"<temp> I am temp in tempo with temptation </temp>";
    NSString *word = @"temp";
    NSError *outError;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"[^<\\/?\\w*>]+(%@\\s)", word] options:0 error:&outError];
    
    NSTextCheckingResult *result = [regex firstMatchInString:evaluate_string options:0 range:NSMakeRange(0, [evaluate_string length])];
    
    if(result) {
        NSLog(@"Found");
    }
    

    How about this puppy:

    </?[a-z][a-z0-9]*[^<>]*>

    I found it in the RegExBuddy Library 🙂