1

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?

4
  • 1
    Do you want to create typescript interfaces/classes based on C# classes ? Because there are better ways to do that... Commented Sep 1, 2017 at 15:00
  • Based on the bare minimum of what you're asking using regex, I would use this instead: public\s*(\w+)\s*(\w+)\s*\{\s*get;\s*set;\s*\} with the following substitution: $2: $1;. This does exactly what you're asking of regex, but I would definitely agree with @TitianCernicova-Dragomir that there is probably a better way to go about this. Commented Sep 1, 2017 at 15:03
  • Why not to use Reflections? You can do it in post-build script. Commented Sep 1, 2017 at 15:05
  • @TitianCernicova-Dragomir Yes that's what I want. I have models in backend fetching by frontEnd and I want to have the same object definition... What would be a better way? Commented Sep 1, 2017 at 15:08

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you! you were right this is a better option!
@user8019820 Glad to help :)
1

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

1 Comment

I just want to do CTRL-H (replace) in visual studio code using a regex expression to go faster. Thanks for the link :)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.