Regular Expressions 101

Community Patterns

C#-ifying classes pasted from JSON

0

Regular Expression
PCRE2 (PHP >=7.3)

/
^\s+public (?<type>[\w\[\]]+) (?<prop_1st>\w)(?<prop_rest>[^\W_]+)(?<prop_opt>_(?<opt_1st>\w)(?<opt_rest>[^\W_]+))* { get; set; }$
/
gm

Description

When pasting JSON payloads, requests or response as C# classes, the casing of property names is not usually idiomatic C#. This regular expression helps transform the property names while adding System.Text.Json.JsonPropertyName attributes to properties to preserve the original names for serialization.

Submitted by Gareth James - a year ago