【问题标题】:ios get a element from nsstringios从nsstring中获取一个元素
【发布时间】:2018-07-05 17:35:06
【问题描述】:

我需要在<caption>标签中找到以“©”开头并在</caption>标签之前结束的字符串。

例如在这个字符串中它将是:

Le pop-up "AENAON hellas made", mode produite en Grèce donc, propose une multi <caption><p></p><img class="size-full wp-image-36678" src="https://vivreaberlin.com/wp-content/uploads/2017/12/dimitrios-ourdoulidis.jpg" alt="" width="960" height="700" /> © Dimitrios Ourdoulidis</caption><br />

Et c'est très agréable de voir une boutique éphémère qui ouvre la voie à de nouveaux designers encore inconnus à Berlin. <br></br><h2>De la mode pointue</h2> <caption><p></p><img class="wp-image-36607 size-full" src="https://vivreaberlin.com/wp-content/uploads/2017/12/big-athens.png" alt="" width="1000" height="670" /> © Dig Athens</caption><br />

© Dimitrios Ourdoulidis

© 挖掘雅典

【问题讨论】:

  • 不幸的是,它仅适用于带有“©”的 1 个元素
  • 在您的问题中您说“我需要找到以“©”开头的字符串作为您在 StackOverflow 中的下一个问题中的建议,请明确您的要求以获得帮助

标签: ios objective-c nsstring


【解决方案1】:

我认为你应该研究这种字符串匹配的正则表达式。

我并没有真正使用 Objective c,但这是我在 Swift 上所做的以使其正常工作。

var regex = ">([^>])*(</caption>)"
var testString = "Le pop-up \"AENAON hellas made\", mode produite en donc, propose une multi <caption><p></p><img class=\"size-full wp-image-36678\" src=\"https://vivreaberlin.com/wp-content/uploads/2017/12/dimitrios-ourdoulidis.jpg\" alt=\"\" width=\"960\" height=\"700\" /> © Dimitrios Ourdoulidis</caption><br />\r\n\r\nEt c'est  de voir une boutique qui ouvre la voie de nouveaux designers encore inconnus  Berlin. <br></br><h2>De la mode pointue</h2> <caption><p></p><img class=\"wp-image-36607 size-full\" src=\"https://vivreaberlin.com/wp-content/uploads/2017/12/big-athens.png\" alt=\"\" width=\"1000\" height=\"670\" /> © Dig Athens</caption><br />"

let regularExpression = try NSRegularExpression(pattern: regex, options: .caseInsensitive)
let matches = regularExpression.matches(in: testString, options: [], range: NSMakeRange(0, (testString as NSString).length))
print(testString)

matches.forEach {
    let range = $0.range
    let strictRange = NSMakeRange(range.lowerBound + 2, range.length - 12)

    print((testString as NSString).substring(with: strictRange) as String)
}

正则表达式/&gt;([^&gt;])*(&lt;/caption&gt;)/匹配结尾&gt;&lt;/caption&gt;

所以对于您的示例字符串,其中一个匹配项将是 &gt; © Dimitrios Ourdoulidis&lt;/caption&gt;

为了克服这个问题,我将匹配范围从字符串的前面减少了 2,从后面减少了 10。

【讨论】:

    【解决方案2】:

    使用常规 @"©{1,1}(.)*(&lt;/){1,1}" 表达式,您可以获得这些子字符串,然后将 "&lt;/" 替换为 "" 将返回您期望的内容

    - (NSMutableArray*)substrings:(NSString*)candidateString{
    
        NSRegularExpression * exp = [[NSRegularExpression alloc]initWithPattern:@"©{1,1}(.)*(</){1,1}" options:NSRegularExpressionDotMatchesLineSeparators error:nil];
    
        NSMutableArray *resultArray = [NSMutableArray array];
        [exp enumerateMatchesInString:candidateString options:NSMatchingWithoutAnchoringBounds range:NSMakeRange(0, candidateString.length) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
            [resultArray addObject:[[candidateString substringWithRange:[result range]] stringByReplacingOccurrencesOfString:@"</" withString:@""]];
        }];
    
        NSLog(@"%@",resultArray);
        return resultArray;
    }
    

    输入:

    NSString* str2 = @"Le pop-up \"AENAON hellas made\", mode produite en Grèce donc, propose une multi <caption><p></p><img class=\"size-full wp-image-36678\" src=\"https://vivreaberlin.com/wp-content/uploads/2017/12/dimitrios-ourdoulidis.jpg\" alt=\"\" width=\"960\" height=\"700\" /> © Dimitrios Ourdoulidis</caption><br />";
    

    输出

    2018-01-26 15:43:30.486182+0100 RegexTestingProject[58921:2174408] ( "\U00a9 Dimitrios Ourdoulidis")

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      • 1970-01-01
      • 2016-10-07
      相关资源
      最近更新 更多