本帖最后由 flashercs 于 2024-10-6 19:16 编辑
- $s = @'
- using System;
-
- public static void _Main0() {
- /*
- *multiline comment
-
- */
- string s = """
- _Main0
- {hello {world {
- }
- """;//singleline comment
-
- Console.WriteLine(s);
- if(a=b){echo 111;/*comment
- */}
- //comment
- }
-
- 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;
- }
- '@
- <#
- $re=[regex]'(?s)\b_Main(?>\w*)(?>\s*)\((?>[^)]*)\)(?>[^{]*)(?<o>\{)(?>/\*.*?\*/|//[^\n]*|"(?>""|\\"|[^"])*"|(?<o>\{)|(?<c-o>\})|.)+?(?(o)!)'
- $ms=$re.Matches($s)
- foreach($m in $ms){
- $gpc=$m.Groups['c']
- $gpc.Captures[$gpc.Count-1].Value
- '---------------'|Write-Host -ForegroundColor Green
- }
- #>
- $re=[regex]'(?s)\b_Main(?>\w*)(?>\s*)\((?>[^)]*)\)(?>[^{]*)(?<o>\{)(?<body>(?>/\*.*?\*/|//[^\n]*|"(?>""|\\"|[^"])*"|(?<o>\{)|(?<-o>\})|.)*?)(?<-o>\})(?(o)!)'
- $ms=$re.Matches($s)
- foreach($m in $ms){
- '---------------'|Write-Host -ForegroundColor Green
- $m.Groups['body'].Value
- '---------------'|Write-Host -ForegroundColor Green
- }
- <# 上面要处理的文本是一些函数定义, 我想用正则表达式来获取函数名中包含Main的函数体内的文本, 即最终执行的结果如下:
-
- string s = """
- _Main0
- {hello {world {
- }
- """;
- Console.WriteLine(s);
-
- string s = """
- _Main2
- {hello {world }
- {
- """;
- return s;
- #>
复制代码
|