Home > Software engineering >  Remove empty line after Replace in Visual Studio
Remove empty line after Replace in Visual Studio

Time:12-22

I have many classes and these classes have properties with attribute [WordColumn("Xxx", 1, typeof(string))]. E.g:

[JsonObject("Сотрудник")]
public class Person
{
    [JsonProperty("firstName")]
    [WordColumn("Имя", 1, typeof(string))]
    public string FirstName { get; set; }
    
    [JsonProperty("lastName")]
    [WordColumn("Фамилия", 1)]
    public string LastName { get; set; }
    
    // ... other properties are omitted for the brevity
}

What I want is a regular expression that can delete all text that starts from [WordColumn and ends with )] and delete empty line which can be left after deletion.

I've tried to write the following regex and it finds all WordColumn:

\[WordColumn.*?\]   

However, when I use it in Visual Studio with Find and Replace, then Replace in Files, tick Use Regular Expression, I leave Replace empty. Then after it leaves empty spaces after Replaces:

[JsonObject("Сотрудник")]
public class Person
{
    [JsonProperty("firstName")]
                               // <- here the empty line remains
    public string FirstName { get; set; }
    
    [JsonProperty("lastName")]
                              // <- here the empty line remains
    public string LastName { get; set; }
}   

I am doing this in Visual Studio 2019 with Replace button.

Is it possible to remove this empty lines after [WordColumn...] was replaced?

CodePudding user response:

Use the regex from @AndersonPimentel comment but with a small change (add ^[\t ]* to remove spaces)

^[\t ]*\[WordColumn.*?\]\r?\n

CodePudding user response:

The proper regular expression for both, Visual Studio for Windows and for Mac, would be

\[WordColumn.*?\]\s*
  • Related