Objective-C stringByReplacingOccurrencesOfString with regex expression

Nth*_*ree -1 html regex objective-c nsstring

我正在尝试使用正则表达式分割包含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:@""];
Run Code Online (Sandbox Code Playgroud)

我想删除span-tags.

有任何想法吗?

dre*_*lax 5

你可以用这种方法做这样的事情:

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

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

在上面的链接中查看文档中可能需要的选项.