【问题标题】:iPhone search text with regexiPhone 用正则表达式搜索文本
【发布时间】:2014-10-21 14:14:38
【问题描述】:

我正在使用这个正则表达式:

<li class=\"b_algo\"><h2><a href=\"(.*?)\"

在NSString 中搜索。我希望能够使用此搜索但添加:

<li class=\"b_algo\">@GGGGG<h2><a href=\"(.*?)\"

@GGGGG 表示无论哪个文本'RegexKitLite 都会忽略它,有可能做这样的事情吗?

【问题讨论】:

  • 错误#1:使用正则表达式解析html。

标签: html ios objective-c regex


【解决方案1】:

例子:

NSString *target = @"<li class=\"b_algo\">@GGGGG<h2><a href=\"http://test.xyz\" junk";
NSString *regEx  = @"<li class=\"b_algo\">@[^<]+<h2><a href=\"([^\"]+)\"";

NSRange  searchedRange = NSMakeRange(0, [target length]);
NSError *error = nil;

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:regEx options:0 error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:target options:0 range: searchedRange];

NSLog(@"target: %@", target);
NSLog(@"regEx:  %@", regEx);
NSLog(@"group1: %@", [target substringWithRange:[match rangeAtIndex:1]]);

输出:

target: <li class="b_algo">@GGGGG<h2><a href="http://test.xyz" junk
regEx:  <li class="b_algo">@[^<]+<h2><a href="(.*?)"
group1: http://test.xyz

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多