Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#正则表达式替换为相同字符串的部分_C#_Regex - Fatal编程技术网

C#正则表达式替换为相同字符串的部分

C#正则表达式替换为相同字符串的部分,c#,regex,C#,Regex,我正在尝试替换生成的代码文件的一部分: public System.Nullable<int> SomeInt { get; set; } public System.Nullable<bool> SomeBool { get; set; } public System.Nullable<bool> SomeOtherBool { get; set; } 我知道代码是等价的,后者只是语法上的糖。但我还是想做,因为它更可读 正则表达

我正在尝试替换生成的代码文件的一部分:

    public System.Nullable<int> SomeInt { get; set; }

    public System.Nullable<bool> SomeBool { get; set; }

    public System.Nullable<bool> SomeOtherBool { get; set; }
我知道代码是等价的,后者只是语法上的糖。但我还是想做,因为它更可读

正则表达式模式很容易编写

System\.Nullable<.*>
System\.可为空
整个事情,还有类似的事情

(?<=System\.Nullable<).*(?=>)
(?与a一起使用将起作用:

string replaced = Regex.Replace(src, @"System\.Nullable<(?<type>.*)>", "${type}?");
string replaced=Regex.Replace(src,@“System\.Nullable”、“${type}?”);

示例:

您使用的是Visual Studio搜索/替换吗?如果是,是哪个版本的VS,因为您尝试过,
Regex.replace()
?不,我正在使用CodeDom生成源文件,然后读取文件并使用Regex API对其执行替换。这一切都是通过代码完成的。@Shaggydog:没问题,很乐意提供帮助。
string replaced = Regex.Replace(src, @"System\.Nullable<(?<type>.*)>", "${type}?");