[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] PowerShell正则平衡组判断是否嵌套

我想判断判断函数 abc 和 cd 是否在cs类中, 也就是嵌套在里面, 下面是gpt帮写的, 问了十几轮都是错的, 求助路过大佬指导, 谢谢

using System;
using System.Text.RegularExpressions;

public class cs
{
        public string cd(string s, string rx)
        {
                //cd
        }

        public static int abc(string s)
        {
                //abc
        }
}

public static class cc
{
        public string gg(string s)
        {
                //gg
        }

        public static int zz(string s, string rx)
        {
                //zz
        }
}
  1. $s = @'
  2. using System;
  3. using System.Text.RegularExpressions;
  4. public class cs
  5. {
  6. public string cd(string s, string rx)
  7. {
  8. //cd
  9. }
  10. public static int abc(string s)
  11. {
  12. //abc
  13. }
  14. }
  15. public static class cc
  16. {
  17. public string gg(string s)
  18. {
  19. //gg
  20. }
  21. public static int zz(string s, string rx)
  22. {
  23. //zz
  24. }
  25. }
  26. '@
  27. $functionName = 'abc'
  28. $pattern = "(?s)^(?:public|private|protected|internal)\s+(?:(?:unsafe\s+)?static|virtual|override|abstract|sealed|extern)\s+(?:\w+\s+)*?" + [regex]::Escape($functionName) + "\s*\([\w\s,]*?\)\s*(?:(?:where|:)\s+[\w\s,:]+)?\s*{\s*`r?\n"
  29. if ([regex]::IsMatch($s, $pattern))
  30. {
  31. Write-Output "函数 $functionName 在类 cs 中"
  32. }
  33. else
  34. {
  35. Write-Output "函数 $functionName 不在类 cs 中"
  36. }
  37. $functionName = 'cd'
  38. $pattern = "(?s)^(?:public|private|protected|internal)\s+(?:(?:unsafe\s+)?static|virtual|override|abstract|sealed|extern)\s+(?:\w+\s+)*?" + [regex]::Escape($functionName) + "\s*\([\w\s,]*?\)\s*(?:(?:where|:)\s+[\w\s,:]*?)?\s*{\s*`r?\n"
  39. if ([regex]::IsMatch($s, $pattern))
  40. {
  41. Write-Output "函数 $functionName 在类 cs 中"
  42. }
  43. else
  44. {
  45. Write-Output "函数 $functionName 不在类 cs 中"
  46. }
复制代码

Set re = New regexp
re.pattern = "public class cs\n{.*?({.*}.*)*}"
re.multiline = True
re.ignorecase = True
cs = re.execute([yourc#code])(0)
If InStr(cs,"public string cd") <> -1 Then
    MsgBox "cd is in cs"
End If
If InStr(cs,"public int abc") <> -1 Then
    MsgBox "abc is in cs"
End If
成不成?

TOP

回复 2# jyswjjgdwtdtj

多谢大佬, 不用正则平衡组不行, 因为复杂的函数里面会有很多{}嵌套, 还有就是函数名前有各种修饰词,也要考虑

TOP

用下面的方法仍然不行
  1. $s = @'
  2. using System;
  3. using System.Text.RegularExpressions;
  4. public class cs
  5. {
  6.     public string cd(string s, string rx)
  7.     {
  8.         //cd
  9.     }
  10.     public static int abc(string s)
  11.     {
  12.         //abc
  13.     }
  14. }
  15. public static class cc
  16. {
  17.     public string gg(string s)
  18.     {
  19.         //gg
  20.     }
  21.     public static int zz(string s, string rx)
  22.     {
  23.         //zz
  24.     }
  25. }
  26. '@
  27. $className = 'cs'
  28. $functionName = 'abc'
  29. $pattern = "(?s)(?<=\bclass\s+$className\s*\{)[^}]*?(?<=\bfunction\s+$functionName[\s\n]*\()([\w\s,]*)[\)]"
  30. $match = [regex]::Match($s, $pattern)
  31. if ($match.Success)
  32. {
  33. Write-Output "函数 $functionName 在类 $className 中"
  34. }
  35. else
  36. {
  37. Write-Output "函数 $functionName 不在类 $className 中"
  38. }
  39. $functionName = 'cd'
  40. $pattern = "(?s)(?<=\bclass\s+$className\s*\{)[^}]*?(?<=\bfunction\s+$functionName[\s\n]*\()([\w\s,]*)[\)]"
  41. $match = [regex]::Match($s, $pattern)
  42. if ($match.Success)
  43. {
  44. Write-Output "函数 $functionName 在类 $className 中"
  45. }
  46. else
  47. {
  48. Write-Output "函数 $functionName 不在类 $className 中"
  49. }
复制代码

TOP

回复 3# 小白龙


    换个vb.net吧 那就不用平衡组了[doge]

TOP

给你出个主意 先写个c#缩进脚本 设置好缩进 然后就不用平衡组 可以依靠每条语句前面的空格来判断

TOP

缩进很好写的 随便拉来个人都行
这种类c的语言找到一一对应的很烦 但是缩进好写的不得了

TOP

回复 6# jyswjjgdwtdtj


    不太懂,

感觉gpt的正则可能哪里有转义的问题了, 记得以前有坛友也遇到过类似的问题

TOP

回复 8# 小白龙


    难点不就是在匹配大括号对吗 所以才要什么平衡组,栈希希的
所以你就加缩进 把每一行的“级别‘标在开头

TOP

把上次的改一下
  1. $s -match '(?<a>\n\s*)(private|public|static|async|(?<b>(\b\S+|\([^()]+\))\s*))*(?<c>class\s+cs\s*(\([^)]*\))*\s*\{(?:[^{}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*?(?(DEPTH)(?!))\})'
  2. $regex='(?<a>\n\s*)(private|public|static|async|(?<b>(\b\S+|\([^()]+\))\s*))*(?<c>(abc|cd)\s*\([^)]*\)\s*\{(?:[^{}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*?(?(DEPTH)(?!))\})'
  3. [regex]::Matches($matches.0, $regex)
复制代码

TOP

回复 10# idwma


    谢谢大佬

$matches.0  这是什么意思, 加这个有一堆的输出, 改成 $matches 直接输出 True

TOP

回复 10# idwma


    大佬, 如果我在函数内加个 {  代码就停不下来了, 一直执行了
  1. $s = @'
  2. using System;
  3. using System.Text.RegularExpressions;
  4. public class cs
  5. {
  6. public string cd(string s, string rx)
  7. {
  8. //cd{
  9. }
  10. public static int abc(string s)
  11. {
  12. //abc{
  13. }
  14. }
  15. public static class cc
  16. {
  17. public string gg(string s)
  18. {
  19. //gg
  20. }
  21. public static int zz(string s, string rx)
  22. {
  23. //zz
  24. }
  25. }
  26. '@
复制代码

TOP

回复 11# 小白龙

$matches.0是cs
这堆可以判断有没有abcd
  1. [regex]::Matches($matches.0, $regex)|%{$_.groups[3].value}
复制代码

TOP

回复 12# 小白龙


    改成这样试试
  1. (?<a>\n\s*)(private|public|static|async|(?<b>(\b\S+|\([^()]+\))\s*))*(?<c>class\s+cs\s*(\([^)]*\))*\s*\{(?:([^{}]+|//[^{}]+\{)|\s+\{\s+(?<DEPTH>)|\s+\}\s+(?<-DEPTH>))*?(?(DEPTH)(?!))\})
复制代码

TOP

本帖最后由 idwma 于 2023-5-22 19:27 编辑

可能会有多行注释和引号里不成对括号的情况再改一下
  1. (?<a>\n\s*)(private|public|static|async|(?<b>(\b\S+|\([^()]+\))\s*))*(?<c>class\s+cs\s*(\([^)]*\))*\s*\{(?>[^{}\/\x22\x27]+|\/\/[^\n]+|/\*(\n|.)*?\*/|[\x22\x27](\n|.)*?[\x22\x27]|[^{}]|\{(?<DEPTH>)|\}(?<-DEPTH>))*?(?(DEPTH)(?!))\})
复制代码

TOP

返回列表