import Foundation
let pattern = #"(?<!^)[ \t]*\r?\n(?=\$)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
$index 57320
$title The vertex-cover polynomial of a graph
$time 1988
$abstract In this paper we define the vertex-cover...
$index 57321
$title Locating stations on rapid transit lines
$time 1978
$index 57322
$title Fast heuristics for large scale covering-location problems
$time 1998
$abstract We propose fast heuristics for large scale...
$index 57323
$title Efficient vector processing on dataflow supercomputer SIGMA-1
$time 2001
$abstract Efficiency in vector handling is the key to obtaining high...
"""#
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