import Foundation
let pattern = #"(?<=(\/\/\[JsonProperty\(Required = Required\.Always\)]\n)|(\/\/\[JsonProperty\(DefaultValueHandling = DefaultValueHandling\.Ignore\)]\n))( public [a-zA-Z]+ )"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
namespace Telegram.Bot.Types.Payments
{
/// <summary>
/// This object contains information about an incoming pre-checkout query
/// </summary>
//[JsonObject(MemberSerialization.OptIn, NamingStrategyType = typeof(SnakeCaseNamingStrategy))]
public sealed record PreCheckoutQuery
{
/// <summary>
/// Unique query identifier
/// </summary>
//[JsonProperty(Required = Required.Always)]
public string Id { get; set; }
/// <summary>
/// User who sent the query
/// </summary>
//[JsonProperty(Required = Required.Always)]
public User From { get; set; }
/// <summary>
/// Three-letter ISO 4217 currency code
/// </summary>
//[JsonProperty(Required = Required.Always)]
public string Currency { get; set; }
/// <summary>
/// Total price in the smallest units of the currency.
/// </summary>
//[JsonProperty(Required = Required.Always)]
public int TotalAmount { get; set; }
/// <summary>
/// Bot specified invoice payload
/// </summary>
//[JsonProperty(Required = Required.Always)]
public string InvoicePayload { get; set; }
/// <summary>
/// Optional. Identifier of the shipping option chosen by the user
/// </summary>
//[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public string ShippingOptionId { get; set; }
/// <summary>
/// Optional. Order info provided by the user
/// </summary>
//[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public OrderInfo OrderInfo { get; set; }
}
}
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #" [JsonPropertyName("")]\n$0"#
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