如何在基于Swift 5.2的RegEx中使用Ungridy标志
使用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",
} ]
}}]
要获得想要的效果,可以将正则表达式更改为以下内容。
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://regex101.com/r/vPGRwk/1