找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
楼主: 小白龙

[问题求助] PowerShell正则平衡组获取{}内多行文本

[复制链接]
发表于 2023-5-27 15:06:26 | 显示全部楼层
本帖最后由 idwma 于 2023-5-27 15:07 编辑

还可以试试这个
  1. function getFuncText($a, $b)
  2. {
  3.         if (!$b) { $b = '\S+' }
  4.         $r = '(?s)public\sclass\s'+$b+'\s*\{.+?(private|public|static|void|\s+)*'+$a+'\s*(\([^)]*\))*\s*\{\s*(?<d>(?>[^{}\/\x22\x27\r\n]+|\/\/[^\r\n]+|/\*.*?\*/|[\x22][^\x22]*[\x22]|[\x27][^\x27]*[\x27]|\r?\n|\{(?<DEPTH>)|\}(?<-DEPTH>))*?(?(DEPTH)(?!)))\}'
  5.         [regex]::Matches($s, $r).groups | ?{ $_.name -eq 'd' } | %{ $_.captures.value }
  6. }
复制代码
 楼主| 发表于 2023-5-27 17:58:31 | 显示全部楼层
回复 16# idwma
多谢大佬, 可以正确输出, 和前面那个原理一样吗? 只是精简了一下吗
 楼主| 发表于 2023-5-27 18:02:26 | 显示全部楼层
本帖最后由 小白龙 于 2023-5-27 18:04 编辑

回复 16# idwma

$s = gc -Path "$HOME\Desktop\abc.txt"

用gc获取文件中的字符串, 多行的结果不正确 ,并在一行了, 把字符串写在代码中没事
  1. $s = @'
  2. using System;

  3. public class xx
  4. {
  5.         public static void Main()
  6.         {
  7.                 string s = "xzc abc(s "(abc)")); dfg()";
  8.                 string s = "xzc abc(s "(abc)")); dfg()";
  9.                 string p = @"}";
  10.                
  11.                 string p = @"}";
  12.                 string p = @"}";
  13.                                 string s = "xzc abc(s "(abc)")); dfg()";
  14.         }
  15.        
  16.         public static string hello(string s, string p)
  17.         {
  18.                 string fullMatch = match.Value;
  19.         }
  20. }

  21. public class zz
  22. {
  23.         public static void Main()
  24.         {
  25.                 string s = "xzc abc(s "(abc)")); dfg()";
  26.                 string p = @"}";
  27.         }
  28.        
  29.         public static string hello(string s, string p)
  30.         {
  31.                 string fullMatch = match.Value;
  32.         }
  33. }
  34. '@
复制代码
发表于 2023-5-27 20:10:37 | 显示全部楼层
回复 17# 小白龙
是什么地方不懂
回复 18# 小白龙
gc得到的是数组,要转成字符
 楼主| 发表于 2023-5-27 20:37:13 | 显示全部楼层
回复 19# idwma

多谢提醒, 加了一个-raw就可以了
 楼主| 发表于 2023-5-27 21:02:42 | 显示全部楼层
回复 19# idwma

大佬, 新的这个正则的执行速度比前面的那个要快多了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 00:58 , Processed in 0.016969 second(s), 7 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表