How to Remove Chunk of Code using Find and Replace from Visual Studio with Regular Expression?

Asked

Viewed 49 times

1

I’m with a c# application that we’re looking to remove code snippets.

One of the situations I have several If/E-s, where Else doesn’t make sense.

Type like this (as an example):

var entidades= db.Entidades.ToList();
if (!entidades.Where(w => w.ID == 1).Any())
{
     db.Entidades.Add(new Entidade{
         Id = 1,
         Existe = 1,
         Descricao = "Descricao"
     })
}
else 
{
     var entidade = entidades.Where(w => w.Id == 1).FirstOrDefault();
     entidade.Existe = 1;
     entidade.Descricao = "Descricao";
}

And this happens in several parts of the code, many even.

So I would like to make a regular expression that removes all the Else.

  • If you had created a good abstraction you would avoid this. Read https://answall.com/q/120931/101. I hate Regex so I can’t help you anymore. And there are other problems in how people use LINQ in EF and nobody even notices.

  • Want to use Regex to remove code? What do you mean? You can give a practical example, even in algorithmic, of how you want to do this?

  • @Maniero , I think they did not understand what I need, I am not discussing the question of good practices in the code, when I hit my eye I saw that there was redundancy in the code. The point is that I have a class with more than 2000 situations equal to this example, and I wanted to automate to not have all this work.

  • @Joãomartins In Visual Studio’s Find and Raplaces tool, in the Replace in Files tab (Shortcut: CTRL + SHIFT + H) you have the option Use Regular Expression, I want to use it to remove all Lses.

  • 1

    Ha, ok, I was missing the point. Thank you.

  • And which Pattern you want to identify?

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.