import Foundation
let pattern = #"(?<=\s)If\s+(.+);"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
If ( x+w>0 And x-w<ScrW() ) And ( y+h>0 And y-h<ScrH() );
Return 1;
Else;
Return 0;
EndIf;
If Cur>Maximum Cur=0;
If Part.image<>Null;
If (dotX+x=>Part.x And dotX+x<Part.x+Part.w) And (doty+y=>Part.y And doty+y<Part.y+Part.h);
// если над этим пикселем есть миддлграунд - то не меняем пиксель
If Not Part.IsMGpixel(x-Part.x+dotX,y-Part.y+doty);
Local TPixmap TempPix= Part.Pixmap;
WritePixel( TempPix, x-Part.x+dotX ,y-Part.y+doty ,pixel);
Part.change=1;
EndIf;
Exit;
EndIf;
EndIf;
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"if ($1)\{"#
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