且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用于多行的 REGEX - powershell

更新时间:2023-01-14 14:25:24

取决于您使用的正则表达式方法.

It depends on what regex method you are using.

如果您使用 .NET Regex::Match,还有第三个参数,您可以在其中定义额外的 regex 选项.在此处使用 [System.Text.RegularExpressions.RegexOptions]::Singleline:

If you use the .NET Regex::Match, there is a third parameter where you can define additional regexoptions. Use [System.Text.RegularExpressions.RegexOptions]::Singleline here:

$html = 
@'
<li> test </li>
</ul>
'@

$regex = '(^.*<li>.*\</ul>)' 

[regex]::Match($html,$regex,[System.Text.RegularExpressions.RegexOptions]::Singleline).Groups[0].Value

如果您想使用 Select-String cmdlet,您必须指定regex 中的单行选项 (?s):

If you want to use the Select-String cmdlet, you have to specifiy the singleline option (?s) within your regex:

$html = 
@'
<li> test </li>
</ul>
'@

$regex = '(?s)(^.*<li>.*\</ul>)' 

$html | Select-String $regex -AllMatches | Select -Expand Matches | select -expand Value