html - 在 NSString 中搜索以特定文本开头并以 "结尾的单词

标签 html objective-c ios arrays nsstring

我有一个包含网页 html 代码的 NSString。 现在,我如何在整个字符串中搜索以 'data-src="http://' 开头并以 "结尾的所有单词? 我会将它们保存在一个数组中。 我的字符串称为 urlPage

PS:我不希望单词包含 'data-src="http://' 和 "。我只想要这两个之间的单词

最佳答案

下面是一些示例代码:

NSString *string;
NSString *pattern;
NSRegularExpression *regex;

string = @" aa data-src=\"http://test1\" cd data-src=\"http://test2\" cd";
pattern = @"data-src=\"http://([^\"]+)\"";

regex = [NSRegularExpression
         regularExpressionWithPattern:pattern
         options:NSRegularExpressionCaseInsensitive
         error:nil];

NSArray *matches = [regex matchesInString:string
                                  options:0
                                    range:NSMakeRange(0, [string length])];

for (NSTextCheckingResult *match in matches) {
    NSRange range = [match rangeAtIndex:1];
    NSLog(@"match: '%@'", [string substringWithRange:range]);
}

NSLog输出:
匹配:'test1'
匹配:'test2'

关于html - 在 NSString 中搜索以特定文本开头并以 "结尾的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9268795/

相关文章:

ios - 自定义类的 Objective-C 多重嵌套 initWith

ios - 在运行时更改 UITableView 行的部分

ios - 我的 CALayer 变换在动画后保持不变,但透视消失了

ios - 下标使用不明确错误

javascript - Bootstrap 折叠 <table>

未读取 Php 文本框数据

html - 使页脚在移动 View 和桌面 View 中都停留在页面底部

ios - CLBeacon : How can I get the distance from the IBeacons?

ios - 如何获取视频完整时长和当前播放时间?

html - 防止两个词之间换行的破折号