[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]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. }
复制代码

回复 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

回复 6# jyswjjgdwtdtj


    不太懂,

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

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

本帖最后由 小白龙 于 2023-5-26 17:45 编辑

回复 17# idwma

刚看到, 多谢大佬   

大佬能帮看看这个吗? 这个太综合 , 把以前的正则都利用起来了
http://www.bathome.net/thread-66246-1-1.html

TOP

返回列表