【问题标题】:NSRegularExpression for stripping HTML Tag用于剥离 HTML 标记的 NSRegularExpression
【发布时间】:2011-02-09 07:13:32
【问题描述】:

我正在开发一个电子书阅读器应用程序。我有整个电子书的 .ePUB 文件,其中电子书的每个主题都是一个 html 文件。我想在应用程序中实现搜索功能。我正在使用 NSRegularExpression 类进行搜索。请考虑以下 html 代码:

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

例如在上面的 html 代码中我只想搜索单词 temp。现在在上面的代码中 temp 出现了 5 次 -> &lt;temp&gt; &lt;/temp&gt; temp tempo 诱惑。我正在寻找一个只能提取整个单词“temp”的正则表达式。我不想考虑html标签&lt;temp&gt; &lt;/temp&gt;中的temp这个词。我也不想考虑节奏和诱惑这个词。

提前致谢

【问题讨论】:

    标签: iphone objective-c regex


    【解决方案1】:

    这是怎么回事?

    [^<\/?\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");
    }
    

    【讨论】:

    • 谢谢雅各布。像魅力一样工作。只是想知道...请您简要解释一下上述正则表达式的工作原理。
    【解决方案2】:

    这只小狗怎么样:

    &lt;/?[a-z][a-z0-9]*[^&lt;&gt;]*&gt;

    我在 RegExBuddy 库中找到了它:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多