用于恢复 VisualStudio 设计器代码的正则表达式
Regex to restore VisualStudio Designer Code
我有一个从反编译的 VisualStudio 项目中恢复的 C# 项目。我正在尝试恢复 .Designer.cs 文件,但反编译文件中的代码格式与 VisualStudio 期望的格式不匹配。
特别是我需要删除临时变量的使用。我正在寻找可以在 VisualStudio 中使用的 Regex 表达式来进行搜索和替换以重新格式化以下类型的代码:
替换:
Label label1 = this.Label1;
Point point = new Point(9, 6);
label1.Location = point;
有:
this.Label1.Location = new Point(9, 6);
替换:
TextBox textBox5 = this.txtYear;
size = new System.Drawing.Size(59, 20);
textBox5.Size = size;
有:
this.txtYear.Size = new System.Drawing.Size(59, 20);
等等
这是适用于您提供的两个示例的正则表达式替换。我确认它在 this online .NET regex tester.
中进行了预期的修改
您可能需要进一步修改此正则表达式以满足您的需要。一方面,我不确定您的文件中的代码有多少变化。如果那些三行代码片段中混入了“普通”C# 代码,那么这个正则表达式只会把它们搞得一团糟。您也没有指定这些三行代码片段在文件中的分隔方式,因此您必须编辑正则表达式,以便它可以找到三行代码片段的开头。例如,如果所有三行代码片段都以两个 Windows 格式的换行符开头,您可以将 \r\n\r\n
添加到正则表达式的开头以检测它们,并添加到替换的开头以便保留它们.
查找正则表达式
[^=]+=\s*([^;]+);\s*\n[^=]+=\s*([^;]+);\s*\n\w+(\.[^=]+=\s*)\w+;
带有空格和注释的版本:
[^=]+=\s* # up to the = of the first line
([^;]+) # first match: everything until…
;\s*\n # the semicolon and end of the first line
[^=]+=\s* # up to the = of the second line
([^;]+) # second match: everything until…
;\s*\n # the semicolon and end of the second line
\w+ # variable name (assumed to be the first line)
(\.[^=]+=\s*) # third match: “.propertyName = ”
\w+ # variable name (assumed to be the second line)
; # semicolon at the end of the line
替换字符串
;
等于等号后的第一行,然后是.propertyName =
,然后是等号后的第二行,最后是分号。
我有一个从反编译的 VisualStudio 项目中恢复的 C# 项目。我正在尝试恢复 .Designer.cs 文件,但反编译文件中的代码格式与 VisualStudio 期望的格式不匹配。
特别是我需要删除临时变量的使用。我正在寻找可以在 VisualStudio 中使用的 Regex 表达式来进行搜索和替换以重新格式化以下类型的代码:
替换:
Label label1 = this.Label1;
Point point = new Point(9, 6);
label1.Location = point;
有:
this.Label1.Location = new Point(9, 6);
替换:
TextBox textBox5 = this.txtYear;
size = new System.Drawing.Size(59, 20);
textBox5.Size = size;
有:
this.txtYear.Size = new System.Drawing.Size(59, 20);
等等
这是适用于您提供的两个示例的正则表达式替换。我确认它在 this online .NET regex tester.
中进行了预期的修改您可能需要进一步修改此正则表达式以满足您的需要。一方面,我不确定您的文件中的代码有多少变化。如果那些三行代码片段中混入了“普通”C# 代码,那么这个正则表达式只会把它们搞得一团糟。您也没有指定这些三行代码片段在文件中的分隔方式,因此您必须编辑正则表达式,以便它可以找到三行代码片段的开头。例如,如果所有三行代码片段都以两个 Windows 格式的换行符开头,您可以将 \r\n\r\n
添加到正则表达式的开头以检测它们,并添加到替换的开头以便保留它们.
查找正则表达式
[^=]+=\s*([^;]+);\s*\n[^=]+=\s*([^;]+);\s*\n\w+(\.[^=]+=\s*)\w+;
带有空格和注释的版本:
[^=]+=\s* # up to the = of the first line
([^;]+) # first match: everything until…
;\s*\n # the semicolon and end of the first line
[^=]+=\s* # up to the = of the second line
([^;]+) # second match: everything until…
;\s*\n # the semicolon and end of the second line
\w+ # variable name (assumed to be the first line)
(\.[^=]+=\s*) # third match: “.propertyName = ”
\w+ # variable name (assumed to be the second line)
; # semicolon at the end of the line
替换字符串
;
等于等号后的第一行,然后是.propertyName =
,然后是等号后的第二行,最后是分号。