回复 3# idwma
替换成前置都能替换成功, 但是前置的关键词情况太多了, 怎样通用呢- #--------------------------------------------------------------------情况1: 函数FUN1返回值类型void
-
- $s1 = @'
- using System;
-
- public class CLA
- {
- void FUN1()
- {
- Console.WriteLine("FUN1");
- }
- }
- '@
-
- #--------------------------------------------------------------------情况2: 返回值类型改为了 string
-
- $s2 = @'
- using System;
-
- public class CLA
- {
- string FUN1()
- {
- return "FUN1";
- }
- }
- '@
-
- #--------------------------------------------------------------------情况3: 加了修饰词 public
-
- $s3 = @'
- using System;
-
- public class CLA
- {
- public void FUN1()
- {
- Console.WriteLine("FUN1");
- }
- }
- '@
-
- #--------------------------------------------------------------------情况4: 加了修饰词 public static
-
- $s4 = @'
- using System;
-
- public class CLA
- {
- public static string FUN1()
- {
- return "FUN1";
- }
- }
- '@
-
- # 使用正则表达式删除指定函数
- $s1 -replace '(?s)void\s+FUN1\s*\(\s*\)\s*\{(?:[^{}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*?(?(DEPTH)(?!))\}'
- $s2 -replace '(?s)string\s+FUN1\s*\(\s*\)\s*\{(?:[^{}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*?(?(DEPTH)(?!))\}'
- $s3 -replace '(?s)public void\s+FUN1\s*\(\s*\)\s*\{(?:[^{}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*?(?(DEPTH)(?!))\}'
- $s4 -replace '(?s)public static string\s+FUN1\s*\(\s*\)\s*\{(?:[^{}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*?(?(DEPTH)(?!))\}'
复制代码
|