首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在基于SWIFT5.2的RegEx中添加非贪婪的搜索标志修饰符

如何在基于SWIFT5.2的RegEx中添加非贪婪的搜索标志修饰符
EN

Stack Overflow用户
提问于 2022-08-26 01:54:51
回答 1查看 39关注 0票数 0

如何在基于SWIFT5.2的RegEx中使用非贪婪标志?

使用Regex101.com来测试它-

链接:https://regex101.com/r/mAvptf/1

当我指定了U标志时,它就能工作了。如果没有U标志,它将返回单个结果块。

如何使用(?U).*?来产生与U标志相同的效果?

代码语言:javascript
运行
复制
// WARNING: You included a flag that Swift doesn't support: U
//          When this flag is set, it inverts the "greediness" of the quantifiers so that they are not greedy by default, but become greedy if followed by '?'.
//          As an alternative, this effect can also be achieved by setting a (?U) modifier setting within the pattern or by a question mark behind a quantifier (e.g. .*?).

操场代码:

代码语言:javascript
运行
复制
import Foundation

// WARNING: You included a flag that Swift doesn't support: U
//          When this flag is set, it inverts the "greediness" of the quantifiers so that they are not greedy by default, but become greedy if followed by '?'.
//          As an alternative, this effect can also be achieved by setting a (?U) modifier setting within the pattern or by a question mark behind a quantifier (e.g. .*?).

let pattern = #"(- examples: \[\{(.*[\n])*).*?(?:\}\}\]$)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
    /**
     Gets a list of custom fields.
     - GET
     - OAuth:
       - name:
     - examples: [{contentType=application/json, example={
  "s" : [ {
    "name" : "name",
  }, {
    "name" : "name",
  } ],
  "fields" : [ {
    "items" : [ "listItems", "listItems" ],
    "name" : "name",
      "message" : "message"
    },
  }, {
    "items" : [ "listItems", "listItems" ],
    "name" : "name",
  } ]
}}]
     - returns: Request<Fields>
     */

    /**
     Gets a
     - examples: [{contentType=application/json, example={
  "fields" : [ {
    "name" : "name",
  } ],
  "fields" : [ {
    "listItems" : [ "listItems", "listItems" ],
    "name" : "name",
  } ]
}}]
     - returns: Request<fields>
     */
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let matches = regex.matches(in: testString, range: stringRange)
var result: [[String]] = []
for match in matches {
    var groups: [String] = []
    for rangeIndex in 1 ..< match.numberOfRanges {
        let nsRange = match.range(at: rangeIndex)
        guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue }
        let string = (testString as NSString).substring(with: nsRange)
        groups.append(string)
    }
    if !groups.isEmpty {
        result.append(groups)
    }
}
print(result)

我试图获得两个单独的结果:Ungreedy工作得很好,但Swift5.2不支持它

预期结果

代码语言:javascript
运行
复制
- examples: [{contentType=application/json, example={
  "s" : [ {
    "name" : "name",
  }, {
    "name" : "name",
  } ],
  "fields" : [ {
    "items" : [ "listItems", "listItems" ],
    "name" : "name",
      "message" : "message"
    },
  }, {
    "items" : [ "listItems", "listItems" ],
    "name" : "name",
  } ]
}}]
EN

回答 1

Stack Overflow用户

发布于 2022-08-26 17:23:15

要获得想要的效果,可以将正则表达式更改为以下内容。

let pattern = #"(- examples: \[\{(.*?[\n])*?).*?(?:\}\}\]$)"#

注意,在第一个和第二个*之后,我添加了一个?。这给表达式与U标志相同的效果。

现在您应该可以访问这两个结果,使用上面的代码和新表达式提供以下输出:

代码语言:javascript
运行
复制
[
    [
        "- examples: [{contentType=application/json, example={\n  \"s\" : [ {\n    \"name\" : \"name\",\n  }, {\n    \"name\" : \"name\",\n  } ],\n  \"fields\" : [ {\n    \"items\" : [ \"listItems\", \"listItems\" ],\n    \"name\" : \"name\",\n      \"message\" : \"message\"\n    },\n  }, {\n    \"items\" : [ \"listItems\", \"listItems\" ],\n    \"name\" : \"name\",\n  } ]\n",
        "  } ]\n"
    ],
    [
        "- examples: [{contentType=application/json, example={\n  \"fields\" : [ {\n    \"name\" : \"name\",\n  } ],\n  \"fields\" : [ {\n    \"listItems\" : [ \"listItems\", \"listItems\" ],\n    \"name\" : \"name\",\n  } ]\n",
        "  } ]\n"
    ]
]

这是https://regex101.com/r/FsJ5gZ/1上的

您甚至可以去掉我添加的第一个?,只需使用以下表达式:

let pattern = #"(- examples: \[\{(.*[\n])*?).*?(?:\}\}\]$)"#

这似乎足以得到你的两个结果。

https://regex101.com/r/vPGRwk/1

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73495338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档