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

[问题求助] 正则取文本把GPT折腾的晕头转向

[复制链接]
发表于 2024-10-6 12:01:54 | 显示全部楼层 |阅读模式
要处理的文本是一些函数定义, 我想用正则表达式来获取函数名中包含Main的函数体内的文本, 最终执行的结果在代码的最后注释中,
需要使用正则表达式来获取, 我用GPT试了不少几十轮, 成功率为0
求路过大佬支招
  1. $s = @'
  2. using System;

  3. public static void _Main0() {
  4.         string s = """
  5. _Main0
  6. {hello {world {
  7. }
  8. """;
  9.         Console.WriteLine(s);
  10. }

  11. public void _func0(string s1)
  12. {
  13.         string s = """
  14. _func0
  15. }hello }world }
  16. """;
  17.         Console.WriteLine(s + s1);
  18. }

  19. public void _func2() {
  20.         string s = """
  21. _func2
  22. }hello }world
  23. """;
  24.         Console.WriteLine(s);
  25. }

  26. public static string _Main2(int i)
  27. {
  28.         string s = """
  29. _Main2
  30. {hello {world }
  31. {
  32. """;
  33.         return s;
  34. }
  35. '@

  36. <# 上面要处理的文本是一些函数定义, 我想用正则表达式来获取函数名中包含Main的函数体内的文本, 即最终执行的结果如下:

  37.         string s = """
  38. _Main0
  39. {hello {world {
  40. }
  41. """;
  42.         Console.WriteLine(s);

  43.         string s = """
  44. _Main2
  45. {hello {world }
  46. {
  47. """;
  48.         return s;
  49. #>
复制代码
 楼主| 发表于 2024-10-6 14:00:47 | 显示全部楼层
我试了几乎所有的ai, 在这个问题面前都躺平了
发表于 2024-10-6 17:35:21 | 显示全部楼层
获取的没错,不知道目的是获取哪些?
 楼主| 发表于 2024-10-6 17:45:08 | 显示全部楼层
本帖最后由 小白龙 于 2024-10-6 17:47 编辑

回复 3# flashercs
获取下面的红色字部分, 这些都是函数名中包含文本Main的函数的函数体, 也就是花括号内的文本

using System;

public static void _Main0() {
        string s = """
_Main0
{hello {world {
}
""";
        Console.WriteLine(s);

}

public void _func0(string s1)
{
        string s = """
_func0
}hello }world }
""";
        Console.WriteLine(s + s1);
}

public void _func2() {
        string s = """
_func2
}hello }world
""";
        Console.WriteLine(s);
}

public static string _Main2(int i)
{
        string s = """
_Main2
{hello {world }
{
""";
        return s;

}
 楼主| 发表于 2024-10-6 17:49:14 | 显示全部楼层
回复 3# flashercs


    大佬能支招一下吗? 应该就是用平衡组正则获取到整个函数, 然后把函数体的文本定义到一个()组中, 然后取这个组的值, 多谢
发表于 2024-10-6 18:52:10 | 显示全部楼层
本帖最后由 flashercs 于 2024-10-6 19:16 编辑
  1. $s = @'
  2. using System;

  3. public static void _Main0() {
  4. /*
  5. *multiline comment

  6. */
  7.         string s = """
  8. _Main0
  9. {hello {world {
  10. }
  11. """;//singleline comment

  12.         Console.WriteLine(s);
  13.     if(a=b){echo 111;/*comment
  14.     */}
  15.     //comment
  16. }

  17. public void _func0(string s1)
  18. {
  19.         string s = """
  20. _func0
  21. }hello }world }
  22. """;
  23.         Console.WriteLine(s + s1);
  24. }

  25. public void _func2() {
  26.         string s = """
  27. _func2
  28. }hello }world
  29. """;
  30.         Console.WriteLine(s);
  31. }

  32. public static string _Main2(int i)
  33. {
  34.         string s = """
  35. _Main2
  36. {hello {world }
  37. {
  38. """;
  39.         return s;
  40. }
  41. '@
  42. <#
  43. $re=[regex]'(?s)\b_Main(?>\w*)(?>\s*)\((?>[^)]*)\)(?>[^{]*)(?<o>\{)(?>/\*.*?\*/|//[^\n]*|"(?>""|\"|[^"])*"|(?<o>\{)|(?<c-o>\})|.)+?(?(o)!)'
  44. $ms=$re.Matches($s)
  45. foreach($m in $ms){
  46. $gpc=$m.Groups['c']
  47. $gpc.Captures[$gpc.Count-1].Value
  48. '---------------'|Write-Host -ForegroundColor Green
  49. }
  50. #>
  51. $re=[regex]'(?s)\b_Main(?>\w*)(?>\s*)\((?>[^)]*)\)(?>[^{]*)(?<o>\{)(?<body>(?>/\*.*?\*/|//[^\n]*|"(?>""|\"|[^"])*"|(?<o>\{)|(?<-o>\})|.)*?)(?<-o>\})(?(o)!)'
  52. $ms=$re.Matches($s)
  53. foreach($m in $ms){
  54. '---------------'|Write-Host -ForegroundColor Green
  55. $m.Groups['body'].Value
  56. '---------------'|Write-Host -ForegroundColor Green
  57. }
  58. <# 上面要处理的文本是一些函数定义, 我想用正则表达式来获取函数名中包含Main的函数体内的文本, 即最终执行的结果如下:

  59.         string s = """
  60. _Main0
  61. {hello {world {
  62. }
  63. """;
  64.         Console.WriteLine(s);

  65.         string s = """
  66. _Main2
  67. {hello {world }
  68. {
  69. """;
  70.         return s;
  71. #>
复制代码
 楼主| 发表于 2024-10-6 18:57:39 | 显示全部楼层
回复 6# flashercs


    我看大佬把 _Main0 函数体内添加了一些干扰文本, 但是最后的执行结果, 并没有全部取出来

结果如下:

echo 111;/*comment
    */
---------------

        string s = """
_Main2
{hello {world }
{
""";
        return s;

---------------
 楼主| 发表于 2024-10-6 19:00:14 | 显示全部楼层
回复 6# flashercs

_Main0函数体应该取出如下的文本
   
/*
*multiline comment

*/
        string s = """
_Main0
{hello {world {
}
""";//singleline comment

        Console.WriteLine(s);
    if(a=b){echo 111;/*comment
    */}
    //comment
发表于 2024-10-6 19:11:31 | 显示全部楼层
回复 8# 小白龙


    上面修改了,换了一种方式
 楼主| 发表于 2024-10-6 20:27:38 | 显示全部楼层
本帖最后由 小白龙 于 2024-10-6 20:40 编辑

回复 9# flashercs

多谢大佬帮助, 实在太复杂了, 问了GPT也没解释清楚,

我想将这个功能定义为一个函数应该怎样改呢?  

例如, 我想取 public修饰词开头, 函数名为_Main0的函数体内的文本, 或整个函数定义的文本(一般来讲, 正则的完整匹配应该就是整个函数定义的文本吧?)

为了让函数更具适应性, 有时函数定义没有用public开头, 而是static开头, 问题来了:
那我就想取 static修饰词开头, 函数名为_Main0的函数体内的文本,

或者有更好的方法吗?
发表于 2024-10-6 22:21:00 | 显示全部楼层
这东西正则怎么能搞定?建议去抄C#编译器的词法分析很语法分析的代码
 楼主| 发表于 2024-10-6 22:24:24 | 显示全部楼层
回复 11# Five66


    大佬能给个示例代码吗? 用楼上大佬的貌似就可以, 但是看不懂
 楼主| 发表于 2024-10-6 22:49:11 | 显示全部楼层
回复 11# Five66


   用gpt, 让他用解析语法也不行, 输出为空, N轮都没戏, 哎
发表于 2024-10-6 23:00:26 | 显示全部楼层
括号套括号很难用正则
不如试试栈 或者括号计数器
发表于 2024-10-6 23:33:14 | 显示全部楼层
回复 13# 小白龙
手把手包教包会
  1. <#
  2. .SYNOPSIS
  3. Get the C# Method body string

  4. .DESCRIPTION
  5. Get the C# Method body string

  6. .PARAMETER Code
  7. C# code

  8. .PARAMETER Method
  9. The method name

  10. .PARAMETER Qualifiers
  11. the qualifiers of the method,i.e. public,static

  12. .EXAMPLE
  13. Get-CSBody -Code $s -Method _Main0 -Qualifiers static,public
  14. Get-CSBody -Code $s -Method _Main2 -Qualifiers static,public,string

  15. .NOTES
  16. General notes
  17. #>
  18. function Get-CSBody {
  19.         param(
  20.                 [string]$Code,
  21.                 [string]$Method,
  22.                 [string[]]$Qualifiers
  23.         )
  24.         $re = [regex]@"
  25. \b${Method}$(-join ($Qualifiers|ForEach-Object{"(?<=\b${_}\b.*)"}))(?s)(?>\s*)\((?>[^)]*)\)(?>[^{]*)(?<o>\{)(?<body>(?>/\*.*?\*/|//[^\n]*|"(?>""|\"|[^"])*"|(?<o>\{)|(?<-o>\})|.)*?)(?<-o>\})(?(o)!)
  26. "@
  27.         $ms = $re.Matches($Code)
  28.         foreach ($m in $ms) {
  29.                 #'---------------'|Write-Host -ForegroundColor Green
  30.                 $m.Groups['body'].Value
  31.                 #'---------------'|Write-Host -ForegroundColor Green
  32.         }
  33. }
  34. Get-CSBody -Code $s -Method _Main0 -Qualifiers static,public
  35. Get-CSBody -Code $s -Method _Main2 -Qualifiers public,string
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-17 03:12 , Processed in 0.022381 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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