【问题标题】:Replacing 'non-tagged' content in a web page替换网页中的“未标记”内容
【发布时间】:2015-04-12 12:27:11
【问题描述】:

我目前正在研究一种替换网页中特定文本的方法,但我不想弄乱任何可能用作标记的内容(即 HTML 本身)。我已经研究了许多方法,包括匹配“”字符(并忽略两者之间的内容),但不幸的是,当网页格式错误并且它们不匹配时,这会中断,或者内容很差,或者在实际文本中嵌入了“”。它也非常慢。

提取特定文本不是目标。相反,我需要用不同的文本替换它。

// 进行编辑以使其更清晰(不知道为什么这个问题我得到了两个 -1)。

1) 这是一个非常简单的例子

<head>
    <title>This is my website</title>
    <link rel="shortcut icon" href="//a.b.c">
    <meta name="twitter:card" content="summary">
    <meta property="og:type" content="website" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script>
        mystuff.ready(function () {    
            mystuff.using("snippets", function () {
                mystuff.snippets.initSnippetRenderer();
            });   
        });
    </script>    
</head>
<body class="question-page new-topbar">
    <noscript><div id="noscript-padding"></div></noscript>
    <div id="notify-container"></div>
    <h1>This is piece of large text</h1>
    <ul>
        <li>Coffee</li>
        <li>Tea</li>
        <li>Milk</li>
    </ul>
</body>

当您打开浏览器时,您希望在浏览器中看到以下内容(我们称之为'感兴趣的文本'):

这是一大段文字 * 咖啡 * 茶 * 牛奶

因此,我感兴趣的关键是我如何确定标签之外的内容(即感兴趣的文本并允许使用 RegEX 搜索和替换它)。

2)。 @Zaph - stringByReplacingOccurrencesOfString:withString:options:range 是不够的,因为它不能直接确定范围是什么。范围取决于文本是否包含在 HTML 标记中,或者是否由标记操作的有效负载) - 参见上面的“1”点。

例如,如果我使用直接替换文本“网站”,那么它会替换标题中的文本,但它也会错误地替换第二个元标记中的术语,这是不行的。

有什么想法,或者有什么我可能想到的可以智能地与 HTML 有效负载而不是支持标签一起工作的东西吗?

【问题讨论】:

  • 提供示例输入和输出,并在这些示例中包含特殊情况。
  • 方法stringByReplacingOccurrencesOfString:withString:options:range: 支持带有选项的正则表达式:NSRegularExpressionSearch。请记住,“前瞻断言”、“否定前瞻断言”、“后瞻断言”和“否定后瞻断言”可能非常强大。请参阅:ICU 用户指南:Regular Expressions
  • 我得到了断言方法。我认为它有它的用途,并且可以有选择地应用(例如,不匹配
  • 这是你的html吗?如果是这样,则将要替换的文本括在 div 中,并为 div 添加标签以进行匹配。示例:&lt;li&gt;&lt;div tag=1&gt;Milk&lt;/div&gt;&lt;/li&gt;

标签: html ios regex parsing


【解决方案1】:

使用带有 Look-behind 和 Look-ahead 断言的正则表达式。

该示例将匹配的文本替换为自身,但包裹在邪恶的表情符号中。重点是演示匹配模式。使用NSRegularExpression 可以更好地控制替换。

解释:

(?&lt;=&gt;) Must be preceded with: &gt;
\\S Must start with a non-whitespace character (the \ has to be escaped)
[^&lt;&gt;]+ Must consist of characters except &lt; and &gt;
(?=&lt;/) Must be followed by &lt;/

NSString *html = <question html>;

NSString *pattern = @"(?<=>)\\S[^<>]+(?=</)";
NSString *replacement = @"?$0?";
html = [html stringByReplacingOccurrencesOfString:pattern
                                       withString:replacement
                                          options:NSRegularExpressionSearch
                                            range:NSMakeRange(0, html.length)]
NSLog(@"html:\n%@", html);

输出:

<head>
    <title>?This is my website?</title>
    <link rel="shortcut icon" href="//a.b.c">
    <meta name="twitter:card" content="summary">
    <meta property="og:type" content="website" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script>
        mystuff.ready(function () {    
            mystuff.using("snippets", function () {
                mystuff.snippets.initSnippetRenderer();
            });   
        });
    </script>    
</head>
<body class="question-page new-topbar">
    <noscript><div id="noscript-padding"></div></noscript>
    <div id="notify-container"></div>
    <h1>?This is piece of large text?</h1>
    <ul>
        <li>?Coffee?</li>
        <li>?Tea?</li>
        <li>?Milk?</li>
    </ul>
</body>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 2017-02-25
    • 1970-01-01
    • 2021-07-12
    相关资源
    最近更新 更多