import Foundation
let pattern = #"\n(?=[^"]+?"\s*$)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
ability,n. 能力,能耐;才能
able,adj. 能; 有能力的;能干的
about,"prep. 关于;大约
n. 大致;粗枝大叶;不拘小节的人
adj. 在附近的;四处走动的;在起作用的
adv. 大约;周围;到处"
above,"prep. 超过;在……上面;在……之上
n. 上文
adj. 上文的
adv. 在上面;在上文"
accident,n. 事故;意外; 意外事件;机遇
accurate,adj. 精确的
ache,"n. 疼痛
vi. 疼痛;渴望"
activity,n. 活动;行动;活跃
actor,n. 男演员;行动者;作用物
actress,n. 女演员
actually,adv. 实际上;事实上
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #" "#
let result = regex.stringByReplacingMatches(in: testString, range: stringRange, withTemplate: substitutionString)
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression