下面要处理的字符串变量$s 内有两个类,分别是 xx类 和 zz类, 每个类下都有 Main函数 和 hello函数
我想获取所有Main函数体的文本,下面的代码执行后, 始终只能取两行, gpt也被我搞晕了, 几十回合都在重复一个代码, 哎
在下对平衡组还是一知半解, 求路过大佬帮助
最好能写成一个函数, 函数名 getFuncText 有两个参数, 第1个函数是函数名, 第二个函数是 类名
用法1: 如果只有第1个参数 函数名 没有第2个参数 类名 那就把下面所有类中的 该函数内的文本全输出
getFuncText "Main"
用法2: 如果既有第1个参数 函数名 又有第2个参数 类名 那就只把该类下的 该函数内的文本输出
getFuncText "Main" "xx"
感觉描述是很清楚的, GPT确理解不了, 可能太难了
另外, 类前面的修饰符会有多种情况: 下面是idwma大佬写的平衡组表达示, 供参考:
$s -match '(?<a>\n\s*)(private|public|static|async|(?<b>(\b\S+|\([^()]+\))\s*))*(?<c>class\s+cs\s*(\([^)]*\))*\s*\{(?:[^{}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*?(?(DEPTH)(?!))\})'
$regex='(?<a>\n\s*)(private|public|static|async|(?<b>(\b\S+|\([^()]+\))\s*))*(?<c>(abc|cd)\s*\([^)]*\)\s*\{(?:[^{}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*?(?(DEPTH)(?!))\})'- $s = @'
- using System;
- public class xx
- {
- public static void Main()
- {
- string s = "xzc abc(s "(abc)")); dfg()";
- string p = @"}";
- string q = @"}{{";
- string[] result = matches(s, p);
- }
-
- public static string hello(string s, string p)
- {
- Match match = Regex.Match(s, p);
- string fullMatch = match.Value;
- }
- }
- public class zz
- {
- public static void Main()
- {
- string s = "xzc abc(s "(abc)")); dfg()";
- string p = @"}";
- string q = @"{{}";
- string[] result = matches(s, p);
- }
-
- public static string hello(string s, string p)
- {
- Match match = Regex.Match(s, p);
- string fullMatch = match.Value;
- }
- }
- '@
- $regex = '(?<=Main\s*\(\)\s*\{)(?<BODY>(?:[^{}]+|(?<OPEN>\{)|(?<-OPEN>\}))*)(?(OPEN)(?!))(?=\})'
- if ($s -match $regex)
- {
- $match = $Matches['BODY']
- Write-Output $match
- }
复制代码 |