背景:我需要在C#源文件中匹配多行模式(?)。 正则expression式将由Powershell操作。 我已经testing过,它在RegexBuddy上运行(使用Dot Matches换行符),但是当我尝试通过powershell使用它时,它不起作用。
正则expression式:
[\s]*(?!\/)\[Role.*?\].*?\(.*?\).*?;
C#代码:
[Role (MethodName ="param")] void doSomething(Param1 Param2);
Powershell代码:
$FunctionPattern="^[\s]*(?!\/)\[Role.*?\].*?\(.*?\).*?;" $FunctionMatch =[regex]::matches($Data,$FunctionPattern) $FunctionMatch | format-table index,length,value -auto
据此,Powershell使用多行,我必须使用构造(?m),但这是行不通的
提前帮助和感谢! (Opps我不能使用grep /专用的分析器/和findstr不没有hacl等多行,因此需要Powershell)
“(?m)”修饰符适用于PowerShell操作符(-match,-replace等),但是您使用的是不使用PowerShell修饰符的.NET RegEx类。 在这种情况下,您可以使用Multiline RegexOptions标志:
[regex]::matches($Data, $FunctionPattern, "Multiline")
但是我不认为dot(“。”)在.Net / PowerShell中匹配换行符。 您需要使用“\ r \ n”来匹配换行符。