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

[问题求助] 正则匹配函数定义前后的文本

[复制链接]
发表于 2024-10-7 08:39:18 | 显示全部楼层 |阅读模式
本帖最后由 小白龙 于 2024-10-7 08:52 编辑

问了gpt几十轮, 仍不得其解, 求路过大佬支招, 感谢
PS: 是我描述的不清楚吗? 用了智谱, gpt, kimi, deepseek,  都是弱智, 唉

感觉用flashercs大佬的下面代码中的正则匹配可以解决, 但是让gpt改, 也没解决
http://www.bathome.net/redirect. ... 9891&pid=284800
-------------------------------------------------------------------------------------------------------
写一个名为addPubText的函数,
函数的参数是一段文本, 里面有一些函数的定义;

函数的功能是:
功能A.用正则判断第一个public的函数定义的前面是否存在一些非函数定义的文本:
如果有:
在这些文本的最前面添加 public static void Main() {
在这些文本的后面添加 }
最后返回处理后的完整文本
如果没有: 则返回空

功能B.用正则判断最后一个public的函数定义的后面是否存在一些非函数定义的文本:
如果有:
在这些文本的最前面添加 public static void Main() {
在这些文本的后面添加 }
最后返回处理后的完整文本
如果没有: 则返回空
  1. $s1 = @'
  2. string Txt = "hello";
  3. string Name = "world";

  4. public static void func1(string s) {
  5.         Console.WriteLine(s);
  6.         out(1);
  7. }

  8. public static void out(object x)
  9. {
  10.         Console.WriteLine(x.ToString());
  11. }
  12. '@
  13. <# 变量$s1经过addPubText函数处理后, 最后要得到的结果如下:
  14. public static void Main(){
  15. string Txt = "hello";
  16. string Name = "world";
  17. }

  18. public static void func1(string s) {
  19.         Console.WriteLine(s);
  20.         out(1);
  21. }

  22. public static void out(object x)
  23. {
  24.         Console.WriteLine(x.ToString());
  25. }
  26. #>

  27. $s2 = @'
  28. public static void func1(string s)
  29. {
  30.         Console.WriteLine(s);
  31.         out(1);
  32. }

  33. public static void out(object x) { Console.WriteLine(x.ToString()); }

  34. string Txt = "hello";
  35. string Name = "world";
  36. '@
  37. <# 变量$s2经过addPubText函数处理后, 最后要得到的结果如下:
  38. public static void func1(string s)
  39. {
  40.         Console.WriteLine(s);
  41.         out(1);
  42. }

  43. public static void out(object x) { Console.WriteLine(x.ToString()); }

  44. public static void Main(){
  45. string Txt = "hello";
  46. string Name = "world";
  47. }
  48. #>
复制代码
 楼主| 发表于 2024-10-7 08:40:04 | 显示全部楼层
gpt的答案:

  1. function addPubText
  2. {
  3.         param (
  4.                 [string]$text
  5.         )
  6.        
  7.         $mainFunction = "public static void Main()`r`n{`r`n"
  8.         $closingBrace = "`r`n}`r`n"
  9.        
  10.         # 功能A: 检查第一个 public 函数定义前的非函数文本
  11.         if ($text -match '^(.*?)\s*public\s+.*?\s*\(.*?\)\s*{')
  12.         {
  13.                 $beforePublic = $matches[1].Trim()
  14.                 if ($beforePublic)
  15.                 {
  16.                         $text = $mainFunction + $beforePublic + $closingBrace + $text
  17.                         return $text
  18.                 }
  19.         }
  20.        
  21.         # 功能B: 检查最后一个 public 函数定义后的非函数文本
  22.         if ($text -match 'public\s+.*?\s*\(.*?\)\s*{.*?}([\s\S]*)$')
  23.         {
  24.                 $afterPublic = $matches[1].Trim()
  25.                 if ($afterPublic)
  26.                 {
  27.                         $text = $text + $mainFunction + $afterPublic + $closingBrace
  28.                         return $text
  29.                 }
  30.         }
  31.        
  32.         return ""
  33. }
  34. # 调用函数
  35. $result1 = addPubText $s1
  36. $result2 = addPubText $s2

  37. # 输出结果
  38. $result1
  39. $result2
复制代码
发表于 2024-10-7 12:48:41 | 显示全部楼层
  1. function Add-PubText {
  2.         param (
  3.                 [ValidateNotNullOrEmpty()]
  4.                 [string]$text
  5.         )
  6.         $reMethod = [regex]'(?ms)^(?>\s*)public\b[^(]*\b\w+\b(?>\s*)\((?>[^)]*)\)(?>[^{]*)(?<o>\{)(?>/\*.*?\*/|//[^\n]*|"(?>""|\"|[^"])*"|(?<o>\{)|(?<-o>\})|.)+?(?(o)!)\s*'
  7.         $ms = $reMethod.Matches($text)
  8.         if ($ms.Count -eq 0) { return '' }
  9.         $prefix = $text.Substring(0, $ms[0].Index)
  10.         $suffix = $text.Substring($ms[0].Index)
  11.         if ($prefix -match '\S') {
  12.                 $prefix = @"
  13. public static void Main()
  14. {
  15. $prefix
  16. }
  17. "@
  18.                 return $prefix + $suffix
  19.         }
  20.         $t = $ms[$ms.Count - 1].Index + $ms[$ms.Count - 1].Length
  21.         $prefix = $text.Substring(0, $t)
  22.         $suffix = $text.Substring($t)
  23.         if ($suffix -match '\S') {
  24.                 $suffix = @"
  25. public static void Main()
  26. {
  27. $suffix
  28. }
  29. "@
  30.                 return $prefix + $suffix
  31.         }
  32.         return ''
  33. }
  34. Add-PubText $s1
  35. Add-PubText $s2
复制代码

评分

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

查看全部评分

 楼主| 发表于 2024-10-8 07:22:57 | 显示全部楼层
回复 3# flashercs


    多谢大佬支招,

用正则很利索, 能否用编程的方法实现? 让gpt转成编程的方法, 还是搞不定
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-16 22:04 , Processed in 0.021827 second(s), 10 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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