找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
楼主: 小白龙

[问题求助] [已解决]用正则表达式中的平衡组删除或修改特定字段

[复制链接]
 楼主| 发表于 2023-5-10 16:20:25 | 显示全部楼层
回复 15# idwma

我测试的这个函数,
DownloadFileAsync
, 一直运行, 停不下来了, 我就改了下面两个红色字的地方
(?<a>\s*)(private|public|static|async|(?<b>(\b\S+|\([^()]+\))\s*)|\s*)*(?<c>DownloadFileAsync\s*\(.*\)\s*\{(?:[^{}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*?(?(DEPTH)(?!))\})', '${a}public static ${b}${c}
  1. $s = @'
  2. // 返回Nullable类型
  3. int? GetNullableInt()
  4. {
  5.     int? result = null;
  6.     return result;
  7. }

  8. // 返回Tuple类型
  9. (string, int) GetPersonInfo()
  10. {
  11.     string name = "张三";
  12.     int age = 20;
  13.     return (name, age);
  14. }

  15. // 返回IEnumerable类型
  16. IEnumerable<int> GetNumbers()
  17. {
  18.     yield return 1;
  19.     yield return 2;
  20.     yield return 3;
  21. }

  22. // 返回Task类型
  23. async Task<string> DownloadFileAsync(string url)
  24. {
  25.     HttpClient client = new HttpClient();
  26.     HttpResponseMessage response = await client.GetAsync(url);
  27.     string content = await response.Content.ReadAsStringAsync();
  28.     return content;
  29. }
  30. '@

  31. $s -replace '(?<a>\s*)(private|public|static|async|(?<b>(\b\S+|\([^()]+\))\s*)|\s*)*(?<c>DownloadFileAsync\s*\(.*\)\s*\{(?:[^{}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*?(?(DEPTH)(?!))\})', '${a}public static ${b}${c}'
复制代码
发表于 2023-5-10 16:58:29 | 显示全部楼层
回复 16# 小白龙
  1. $s -replace '(?<a>\n\s*)(private|public|static|async|(?<b>(\b\S+|\([^()]+\))\s*))*(?<c>DownloadFileAsync\s*\([^)]*\)\s*\{(?:[^{}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*?(?(DEPTH)(?!))\})','${a}public static ${b}${c}'
复制代码

评分

参与人数 1技术 +1 收起 理由
小白龙 + 1 乐于助人

查看全部评分

 楼主| 发表于 2023-5-10 17:12:36 | 显示全部楼层
回复 17# idwma


    那个函数修改可以了, 我把正则中的函数名改为另一个  IEnumerable<int>  测试没有效果
发表于 2023-5-10 17:16:08 | 显示全部楼层
回复 18# 小白龙


    把你改的发出来看看
 楼主| 发表于 2023-5-10 17:34:44 | 显示全部楼层
回复 19# idwma
  1. $s = @'
  2. // 返回Nullable类型

  3. // 返回IEnumerable类型
  4. IEnumerable<int> GetNumbers()
  5. {
  6.     yield return 1;
  7.     yield return 2;
  8.     yield return 3;
  9. }
  10. '@

  11. $s -replace '(?<a>\s*)(private|public|static|async|(?<b>(\b\S+|\([^()]+\))\s*)|\s*)*(?<c>GetNumbers\s*\(.*\)\s*\{(?:[^{}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*?(?(DEPTH)(?!))\})', '${a}public static ${b}${c}'
复制代码
 楼主| 发表于 2023-5-10 17:38:49 | 显示全部楼层
回复 19# idwma


    我让gpt写了一下注释
  1. $s -replace '(?x)                                                                # 开启忽略空格和注释选项
  2.                                 (?<a>\n\s*)                                                # 匹配换行符和空格并捕获到名为"a"的组中
  3.                                 (private|public|static|async|(?<b>(\b\S+|\([^()]+\))\s*))*
  4.                                                                                                 # 匹配访问修饰符和方法类型,并将它们捕获到名为"b"的组中
  5.                                 (?<c>                                                        # 匹配方法名和参数列表,并将其捕获到名为"c"的组中
  6.                                         DownloadFileAsync\s*                # 匹配方法名
  7.                                         \([^)]*\)\s*                                # 匹配参数列表
  8.                                         \{(?:[^{}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*?(?(DEPTH)(?!))\}
  9.                                                                                                 # 匹配方法体
  10.                                 )',
  11. '${a}public static ${b}${c}' # 将匹配到的代码块替换为公共静态方法,并确保代码可执行
复制代码
发表于 2023-5-10 17:43:58 | 显示全部楼层
回复 20# 小白龙


    再看看17楼
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 02:09 , Processed in 0.021156 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表