import Foundation
let pattern = #"modelBuilder\.Entity<(?'type'\w+)>\(builder =>\n\s*\{(?'builder'[\w\W]+?)\}\);"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
modelBuilder.Entity<Foobar>(builder =>
{
builder.HasKey(e => e.Id).HasName("PRIMARY");
builder
.ToTable("foobar")
.UseCollation("utf8mb4_0900_ai_ci");
builder.HasIndex(e => e.A, "A").IsUnique();
builder.Property(e => e.D).HasColumnType("datetime");
builder.Property(e => e.E)
.HasMaxLength(255)
.UseCollation("utf8mb4_unicode_ci");
builder.Property(e => e.N)
.HasMaxLength(255)
.UseCollation("utf8mb4_unicode_ci");
builder.HasOne(d => d.ANav).WithOne(p => p.BNav)
.HasForeignKey<Foobar>(d => d.Leerling)
.HasConstraintName("Foobar_fk");
});
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"public class $1Mapping : IEntityTypeConfiguration<$1>\n{\n public void Configure(EntityTypeBuilder<$1> builder)\n {\n$2\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