import Foundation
let pattern = #"\b(??px)(?<!(?<!\S)\.)(\d*)(\d)(?:\.(\d+))?px\b"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
font-size: 10px;
border: 0px;
margin-top: 4px; // dont touch
margin-bottom: 12px;
margin-right: 50%;
margin-left: -12px;
padding-left: 120px;
padding-right: 8px;
border-radius: calc(50% - 1px); // dont touch
grid-template-columns: 1fr 44px 1fr;
margin-top: 36.4px;
border-width: 10.5px;
border-width: .3px; // dont touch
border-width: .6px; // dont touch
border-width: 3.3px; // dont touch
border-width: 6.6px 4px 8000px .34px;
margin-left: -1px; // dont touch
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"$1.$2$3rem"#
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