Objective-C stringByReplacingOccurrencesOfString 与正则表达式

发布于 2024-12-25 13:20:03 字数 402 浏览 2 评论 0原文

我正在尝试使用正则表达式拆分包含 html 代码的字符串:

NSString* regex = @"<.*?>";
NSString* html = @"<span class="test">Test1</span><span class="test">Test2</span><span class="test">Test3</span><span class="test">Test4</span>";

html = [html stringByReplacingOccurrencesOfString:regex withString:@""];

我想删除 span 标签。

有什么想法吗?

I'm trying to split my string containing html code with a regex expression:

NSString* regex = @"<.*?>";
NSString* html = @"<span class="test">Test1</span><span class="test">Test2</span><span class="test">Test3</span><span class="test">Test4</span>";

html = [html stringByReplacingOccurrencesOfString:regex withString:@""];

I want to delete the span-tags.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与酒说心事 2025-01-01 13:20:03

您可能可以使用 此方法

NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:@"<.*?>"
                                                                    options:0
                                                                      error:NULL];

NSString *result = [re stringByReplacingMatchesInString:html
                                                options:0
                                                  range:NSMakeRange(0, [html length])
                                           withTemplate:@""];

在上面链接的文档中检查您可能需要哪些选项。

You could probably do something like this with this method:

NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:@"<.*?>"
                                                                    options:0
                                                                      error:NULL];

NSString *result = [re stringByReplacingMatchesInString:html
                                                options:0
                                                  range:NSMakeRange(0, [html length])
                                           withTemplate:@""];

Check what options you may need in the documentation in the link above.

坠似风落 2025-01-01 13:20:03

如果您的输入是 HTML,则使用 HTML PARSER

用正则表达式“解析” HTML 是徒劳的。请注意,SO 上有很多问题描述了 iO/OSX 上的 HTML 解析。

If your input is HTML, the use an HTML PARSER.

"parsing" HTML with a regular expression is an exercise in futility. Note that there are plenty of questions on SO that describe HTML parsing on iO/OSX.

ゝ杯具 2025-01-01 13:20:03

这只是删除了 <和>字符以及它们之间的所有内容,我认为这就足够了:

 (NSString *) stripTags:(NSString *)str
{
    NSMutableString *ms = [NSMutableString stringWithCapacity:[str length]];

    NSScanner *scanner = [NSScanner scannerWithString:str];
    [scanner setCharactersToBeSkipped:nil];
    NSString *s = nil;
    while (![scanner isAtEnd])
    {
        [scanner scanUpToString:@"<" intoString:&s];
        if (s != nil)
            [ms appendString:s];
        [scanner scanUpToString:@">" intoString:NULL];
        if (![scanner isAtEnd])
            [scanner setScanLocation:[scanner scanLocation]+1];
        s = nil;
    }

    return ms;
}

This just removes < and > characters and everything between them, which I suppose is sufficient:

 (NSString *) stripTags:(NSString *)str
{
    NSMutableString *ms = [NSMutableString stringWithCapacity:[str length]];

    NSScanner *scanner = [NSScanner scannerWithString:str];
    [scanner setCharactersToBeSkipped:nil];
    NSString *s = nil;
    while (![scanner isAtEnd])
    {
        [scanner scanUpToString:@"<" intoString:&s];
        if (s != nil)
            [ms appendString:s];
        [scanner scanUpToString:@">" intoString:NULL];
        if (![scanner isAtEnd])
            [scanner setScanLocation:[scanner scanLocation]+1];
        s = nil;
    }

    return ms;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文