NSRegularExpression 找不到捕获组匹配项
NSRegularExpression cannot find capturing group matches
我正在尝试使用一种正则表达式模式解析字符串。
这是模式:
(\")(.+)(\")\s*(\{)
这里是要解析的文本:
"base" {
我想找到这 4 个捕获组:
1. "
2. base
3. "
4. {
我正在使用以下代码尝试捕获这些组
class func matchesInCapturingGroups(text: String, pattern: String) -> [String] {
var results = [String]()
let textRange = NSMakeRange(0, count(text))
var index = 0
if let matches = regexp(pattern)?.matchesInString(text, options: NSMatchingOptions.ReportCompletion, range: textRange) as? [NSTextCheckingResult] {
for match in matches {
// this match = <NSExtendedRegularExpressionCheckingResult: 0x7fac3b601fd0>{0, 8}{<NSRegularExpression: 0x7fac3b70b5b0> (")(.+)(")\s*(\{) 0x1}
results.append(self.substring(text, range: match.range))
}
}
return results
}
不幸的是,它只能找到范围 (0, 8)
等于:"base" {
的一组。所以它找到一组是整个字符串而不是 4 组。
是否可以使用 NSRegularExpression
获取这些组?
是的,当然可以。您只需更改当前查找实际组的逻辑:
func matchesInCapturingGroups(text: String, pattern: String) -> [String] {
var results = [String]()
let textRange = NSMakeRange(0, text.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
do {
let regex = try NSRegularExpression(pattern: pattern, options: [])
let matches = regex.matchesInString(text, options: NSMatchingOptions.ReportCompletion, range: textRange)
for index in 1..<matches[0].numberOfRanges {
results.append((text as NSString).substringWithRange(matches[0].rangeAtIndex(index)))
}
return results
} catch {
return []
}
}
let pattern = "(\")(.+)(\")\s*(\{)"
print(matchesInCapturingGroups("\"base\" {", pattern: pattern))
你实际上只得到 1 个匹配项。你必须进入那场比赛,在那里你会找到被俘虏的团体。请注意,我省略了第一组,因为第一组代表了整场比赛。
这将输出
[""", "base", """, "{"]
记下转义的正则表达式字符串并确保您使用的是同一字符串。
我正在尝试使用一种正则表达式模式解析字符串。
这是模式:
(\")(.+)(\")\s*(\{)
这里是要解析的文本:
"base" {
我想找到这 4 个捕获组:
1. "
2. base
3. "
4. {
我正在使用以下代码尝试捕获这些组
class func matchesInCapturingGroups(text: String, pattern: String) -> [String] {
var results = [String]()
let textRange = NSMakeRange(0, count(text))
var index = 0
if let matches = regexp(pattern)?.matchesInString(text, options: NSMatchingOptions.ReportCompletion, range: textRange) as? [NSTextCheckingResult] {
for match in matches {
// this match = <NSExtendedRegularExpressionCheckingResult: 0x7fac3b601fd0>{0, 8}{<NSRegularExpression: 0x7fac3b70b5b0> (")(.+)(")\s*(\{) 0x1}
results.append(self.substring(text, range: match.range))
}
}
return results
}
不幸的是,它只能找到范围 (0, 8)
等于:"base" {
的一组。所以它找到一组是整个字符串而不是 4 组。
是否可以使用 NSRegularExpression
获取这些组?
是的,当然可以。您只需更改当前查找实际组的逻辑:
func matchesInCapturingGroups(text: String, pattern: String) -> [String] {
var results = [String]()
let textRange = NSMakeRange(0, text.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
do {
let regex = try NSRegularExpression(pattern: pattern, options: [])
let matches = regex.matchesInString(text, options: NSMatchingOptions.ReportCompletion, range: textRange)
for index in 1..<matches[0].numberOfRanges {
results.append((text as NSString).substringWithRange(matches[0].rangeAtIndex(index)))
}
return results
} catch {
return []
}
}
let pattern = "(\")(.+)(\")\s*(\{)"
print(matchesInCapturingGroups("\"base\" {", pattern: pattern))
你实际上只得到 1 个匹配项。你必须进入那场比赛,在那里你会找到被俘虏的团体。请注意,我省略了第一组,因为第一组代表了整场比赛。
这将输出
[""", "base", """, "{"]
记下转义的正则表达式字符串并确保您使用的是同一字符串。