Board logo

标题: [问题求助] PowerShell正则平衡组判断是否嵌套 [打印本页]

作者: 小白龙    时间: 2023-5-21 10:02     标题: 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. }
复制代码

作者: jyswjjgdwtdtj    时间: 2023-5-21 10:12

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
成不成?
作者: 小白龙    时间: 2023-5-21 10:16

回复 2# jyswjjgdwtdtj

多谢大佬, 不用正则平衡组不行, 因为复杂的函数里面会有很多{}嵌套, 还有就是函数名前有各种修饰词,也要考虑
作者: 小白龙    时间: 2023-5-21 10:29

用下面的方法仍然不行
  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. }
复制代码

作者: jyswjjgdwtdtj    时间: 2023-5-21 10:31

回复 3# 小白龙


    换个vb.net吧 那就不用平衡组了[doge]
作者: jyswjjgdwtdtj    时间: 2023-5-21 10:34

给你出个主意 先写个c#缩进脚本 设置好缩进 然后就不用平衡组 可以依靠每条语句前面的空格来判断
作者: jyswjjgdwtdtj    时间: 2023-5-21 10:35

缩进很好写的 随便拉来个人都行
这种类c的语言找到一一对应的很烦 但是缩进好写的不得了
作者: 小白龙    时间: 2023-5-21 10:39

回复 6# jyswjjgdwtdtj


    不太懂,

感觉gpt的正则可能哪里有转义的问题了, 记得以前有坛友也遇到过类似的问题
作者: jyswjjgdwtdtj    时间: 2023-5-21 10:43

回复 8# 小白龙


    难点不就是在匹配大括号对吗 所以才要什么平衡组,栈希希的
所以你就加缩进 把每一行的“级别‘标在开头
作者: idwma    时间: 2023-5-21 17:24

把上次的改一下
  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)
复制代码

作者: 小白龙    时间: 2023-5-21 18:55

回复 10# idwma


    谢谢大佬

$matches.0  这是什么意思, 加这个有一堆的输出, 改成 $matches 直接输出 True
作者: 小白龙    时间: 2023-5-21 19:27

回复 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. '@
复制代码

作者: idwma    时间: 2023-5-21 19:43

回复 11# 小白龙

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

作者: idwma    时间: 2023-5-21 20:46

回复 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)(?!))\})
复制代码

作者: idwma    时间: 2023-5-22 19:09

本帖最后由 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)(?!))\})
复制代码

作者: jyswjjgdwtdtj    时间: 2023-5-22 22:12

本帖最后由 jyswjjgdwtdtj 于 2023-5-22 22:16 编辑

来个很low的
f = False
j = ""
For Each i In Split(code,vbCrLf)
If i = "public class cs" Then
f = True
j = i
Else
j = j + vbCrLf + i
If f = True And i = "}" Then Exit For
End If
Next
If InStr(j,"public string cd(") > -1 Then MsgBox "cd在cs里"

如果你的缩进做的很好的话……
作者: idwma    时间: 2023-5-23 20:47

再改一下10楼的用命名组合并到一条正则里,不用过两次了
  1. $r='(?<a>\n\s*)(private|public|static|async|(?<b>(\b\S+|\([^()]+\))\s*))*(?<c>class\s+cs\s*(\([^)]*\))*\s*\{(?>[^{}\/\x22\x27]+(?<d>cd|abc)(\s*\([^)]*\))*|[^{}\/\x22\x27]+|\/\/[^\n]+|/\*(\n|.)*?\*/|[\x22\x27](\n|.)*?[\x22\x27]|[^{}]|\{(?<DEPTH>)|\}(?<-DEPTH>))*?(?(DEPTH)(?!))\})'
  2. [regex]::Matches($s, $r).groups|?{$_.name -eq 'd'}|%{$_.captures.value}
复制代码

作者: 小白龙    时间: 2023-5-26 17:29

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

回复 17# idwma

刚看到, 多谢大佬   

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




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2