import Foundation
let pattern = #"(?<!<color=green>|[a-zA-Z0-9])(int|void|float|bool)(?!<\/color>|[a-zA-Z0-9])"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
pulic int Example(float x, bool y, int z)
{
return y ? (int)x : z;
}
public <color=green>int</color> ExamepleAlreadyReplaced(<color=green>float</color> x, <color=green>bool</color> y, <color=green>int</color> z)
{
return y ? (<color=green>int</color>)x : z;
}
public void EdgeCaseKeyWordsInNames()
{
var someint = int.MinValue;;
var somefloat = float.MaxValue;
var somebool = false;
var somevoid = () => { };
var int2 = 0;
var intX = 0;
}
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"<color=green>$1</color>"#
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