如何在基于SWIFT5.2的RegEx中使用非贪婪标志?
使用Regex101.com来测试它-
链接:https://regex101.com/r/mAvptf/1
当我指定了U
标志时,它就能工作了。如果没有U
标志,它将返回单个结果块。
如何使用(?U)
或.*?
来产生与U
标志相同的效果?
// 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. .*?).
操场代码:
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不支持它
预期结果
- examples: [{contentType=application/json, example={
"s" : [ {
"name" : "name",
}, {
"name" : "name",
} ],
"fields" : [ {
"items" : [ "listItems", "listItems" ],
"name" : "name",
"message" : "message"
},
}, {
"items" : [ "listItems", "listItems" ],
"name" : "name",
} ]
}}]
发布于 2022-08-26 17:23:15
要获得想要的效果,可以将正则表达式更改为以下内容。
let pattern = #"(- examples: \[\{(.*?[\n])*?).*?(?:\}\}\]$)"#
注意,在第一个和第二个*
之后,我添加了一个?
。这给表达式与U
标志相同的效果。
现在您应该可以访问这两个结果,使用上面的代码和新表达式提供以下输出:
[
[
"- 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://stackoverflow.com/questions/73495338
复制相似问题