J-R Choiniere
J-R Choiniere

Reputation: 694

Regex c# property to typescript field

I would like to create a regex that replaces c# properties in typescript fields:

public int ItemOfferId { get; set; } would become itemOffer: int;

I'm stuck in the first step: I just want to remove the prefix public int and the suffix { get; set; } in a single regex. Expected : ItemOfferId

For the moment I have (public ([a-z]+) (?=([A-Za-z]+))\w+ { get; set; }) which is giving me ItemOfferId in group3 but I wonder how to ignore it, like creating a hole?

Upvotes: 1

Views: 133

Answers (3)

J-R Choiniere
J-R Choiniere

Reputation: 694

public ((?!\s).)+|{ get; set; }

Upvotes: 0

Felype
Felype

Reputation: 3136

First, I would like to point out that maybe regexing code into code is a bad idea but you can do it like this:

Replace

[public|private|internal]{0,}\s+(\w+)\s+(\w+)\s{0,}\{\s{0,}get\s{0,};\s{0,}set\s{0,};\s{0,}\}

with

$2: $1

Explanation: https://regex101.com/r/TCKchv/2

Upvotes: 1

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249466

If you want to generate typescript interfaces based on your C# types I would recommend TypeLite. I use in my current project with good results. It uses T4 templates which you can customize to generate Typescript definitions, I found it sufficiently customizable and very useful.

Upvotes: 1

Related Questions