在 swift 中检索与正则表达式匹配的子字符串数组
Retrieve array of substring matched with regex in swift
有没有办法在字符串中检索以@符号为前缀的单词数组?
"@City == xyz AND @Hobby == gardening" -> ["@City","@Hobby"]
我尝试了以下两种方法,但都返回一个空数组。
let regexp = "/@\w*/g"
func matches(for regex: String, in text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex)
let results = regex.matches(in: text,range: NSRange(text.startIndex..., in: text))
return results.map {
String(text[Range([=11=].range, in: text)!])
}
} catch let error {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
extension String {
func regex (pattern: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options(rawValue: 0))
let nsstr = self as NSString
let all = NSRange(location: 0, length: nsstr.length)
var matches : [String] = [String]()
regex.enumerateMatches(in: self, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: all) {
(result : NSTextCheckingResult?, _, _) in
if let r = result {
let result = nsstr.substring(with: r.range) as String
matches.append(result)
}
}
return matches
} catch {
return [String]()
}
}
}
正如@jnpdx 在评论中暗示的那样,您的根本问题是您的 regexp
字符串包含来自另一种语言的控制元素。以下应该可以解决您的问题:
let regexp = "@\w*"
您还会陷入不必要的 try-catch 语句和基于 Objective-C 及其相关类型转换的过时 API。应执行以下操作:
func matches(for regex: String, in text: String) -> [String] {
var result = [String]()
var startIndex = text.startIndex
let endIndex = text.endIndex
while let range = text.range(of: regex,
options: .regularExpression,
range: startIndex ..< endIndex)
{
result.append(String(text[range]))
startIndex = range.upperBound
}
return result
}
有没有办法在字符串中检索以@符号为前缀的单词数组?
"@City == xyz AND @Hobby == gardening" -> ["@City","@Hobby"]
我尝试了以下两种方法,但都返回一个空数组。
let regexp = "/@\w*/g"
func matches(for regex: String, in text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex)
let results = regex.matches(in: text,range: NSRange(text.startIndex..., in: text))
return results.map {
String(text[Range([=11=].range, in: text)!])
}
} catch let error {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
extension String {
func regex (pattern: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options(rawValue: 0))
let nsstr = self as NSString
let all = NSRange(location: 0, length: nsstr.length)
var matches : [String] = [String]()
regex.enumerateMatches(in: self, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: all) {
(result : NSTextCheckingResult?, _, _) in
if let r = result {
let result = nsstr.substring(with: r.range) as String
matches.append(result)
}
}
return matches
} catch {
return [String]()
}
}
}
正如@jnpdx 在评论中暗示的那样,您的根本问题是您的 regexp
字符串包含来自另一种语言的控制元素。以下应该可以解决您的问题:
let regexp = "@\w*"
您还会陷入不必要的 try-catch 语句和基于 Objective-C 及其相关类型转换的过时 API。应执行以下操作:
func matches(for regex: String, in text: String) -> [String] {
var result = [String]()
var startIndex = text.startIndex
let endIndex = text.endIndex
while let range = text.range(of: regex,
options: .regularExpression,
range: startIndex ..< endIndex)
{
result.append(String(text[range]))
startIndex = range.upperBound
}
return result
}