Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Powershell多行正则表达式_Regex_Powershell_Powershell 2.0 - Fatal编程技术网

Regex Powershell多行正则表达式

Regex Powershell多行正则表达式,regex,powershell,powershell-2.0,Regex,Powershell,Powershell 2.0,我试图从以下示例中获取行的完整错误: date-time-somemethod EXC-somenumber-sometext R:System.NullReferenceException:对象引用未设置为对象的实例。 在somepath.cs:line somenumber System.NullReferenceException:Object reference未设置为对象实例的sometext处。 在Somepath中的sometext处。cs:行somenumber 从那以后,我想

我试图从以下示例中获取行的完整错误:

date-time-somemethod EXC-somenumber-sometext R:System.NullReferenceException:对象引用未设置为对象的实例。
在somepath.cs:line somenumber System.NullReferenceException:Object reference未设置为对象实例的sometext处。
在Somepath中的sometext处。cs:行somenumber
从那以后,我想在
EXC
cs:line somenumber
之后获取所有内容

01/01/01日期(模式)(状态)(somenumber)(姓名+错误)
,在这里通常会有一个新的行,以错误消息继续,以字符cs:line(数字)结尾

我设法获得了错误消息,因为它总是以EXC开头(因此regex是
EXC.*
),但是我无法获得带有代码的完整消息。我只能访问PowerShell 2.0,我使用以下公式:

$Filecontent = [io.file]::Readalltext("path to file")
$filecontent | select-string 'EXC .*' -allmatches |
  foreach {$_.Matches} | Foreach {$_.Value} > errors.txt
我需要的是得到完整的行号错误,但我有适当的正则表达式的问题。我不关心的日期,时间,模式,正则表达式应该得到EXC状态,并采取完整的消息行

在使用regex'EXC.*\n.*cs:line[0-9]{0,99}之后,它会发现那些在一行结束后带有错误消息的消息,但是,有时我还想捕获更多的后续行。 有什么想法吗?

如果您将错误(堆栈跟踪)定义为

  • 以第1列中的非空白字符开头
  • 跨越多条线
  • 属于错误的每一行都缩进3个空格
然后,用于捕获此类块的正则表达式如下所示:

(?m)^\S.*(\s*^   \S.*)+
在检索完完整的堆栈跟踪块后,您可以在第二步中使用以下方法选择行号:

at (.*?) in (.*?):line (\d+)

表达式分解为:

(?m)         # inline flag: multiline mode
^            # start-of-line
\S           # a non-whitespace character
.*           # anything up to the end of the line
(            # group 1
  \s*        #   any number of whitespace (this matches newline character)
  ^          #   start-of-line
             #   3 spaces
  \S         #   a non-whitespace character
  .*         #   anything up to the end of the line
)+           # end of group 1, repeat at least once


比较:

formula中的打字错误,修复:$Filecontent=[io.file]::Readalltext(“文件路径”)$Filecontent |选择字符串'EXC'.-allmatches | foreach{$\.Matches}| foreach{$\.Value}>错误。txtI将其定义如下:-以EXC开头,一个空格-跨越多行-以cs:line结尾(数字最多四位)我的表达式捕获了其中的一个超集。如果需要的话,可以使其更加具体,例如,将
EXC
放在适当的位置。或者让
Select String
首先获取所有堆栈跟踪,然后通过管道将它们传递到
Where Object
。有很多方法可以做到这一点。嗯,我可能做错了什么,但是当我使用“^\S.*(\S*^\S.*)+”时,它不会检索任何内容。在()内部有我理解的3个空格?是的,三个空格。你注意到“多行”部分了吗?我认为
Select String
没有开关,但是你可以在regex本身中用
(?m)
,比如
(?m)^\S.\S.*+
。这个开关很关键,否则你不能在同一个表达式中有多个
^
。太好了,非常感谢Tomalak。请你向我解释一下这个正则表达式,因为我还在学习,我发现\s代表空白,而\s代表非空白,它不清楚它是如何工作的,但是这个公式:select string'(?m)^\S.*(\S*^\S.*)+'-allmatches | foreach{{$\.Matches}| foreach{$\.Value}>file.txt完成作业(所有类型的错误以及所有新行和代码)。