import Foundation
let pattern = #"(.{1,80}\S)(?:[\r\n\f\v ]+|$)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
--- 0---|--- 10---|--- 20---|--- 30---|--- 40---|--- 50---|--- 60---|--- 70---|--- 80---|--- 90---|
123456789|123456789|123456789|123456789|123456789|123456789|1 23456789|123456789|123456789|123456789|
SELECT
tbl_Hex.Hex_ID
,tbl_Generic_Tool.Generic_Code AS [Generic Code]
,tbl_ref_Generic_Type.Generic_Type AS [Generic Type]
FROM tbl_ref_Location
RIGHT JOIN ((tbl_ref_Generic_Type
INNER JOIN tbl_Generic_Tool ON tbl_ref_Generic_Type.Generic_Type_ID = tbl_Generic_Tool.Generic_Type_ID)
INNER JOIN (tbl_ref_Building
INNER JOIN tbl_Hex ON tbl_ref_Building.Building_ID = tbl_Hex.Building_ID) ON tbl_Generic_Tool.Generic_ID = tbl_Hex.Generic_ID) ON tbl_ref_Location.Location_ID = tbl_Hex.Location_ID
GROUP BY
tbl_Hex.Hex_ID
,tbl_Hex.Hex_No
,tbl_ref_Building.Building_Abbrev
,tbl_ref_Location.Location
,tbl_Generic_Tool.Generic_Code
,tbl_ref_Generic_Type.Generic_Type
ORDER BY
tbl_Hex.Hex_No;
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"$1\r\n"#
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