我在powershell中使用正则表达式有一点问题.我的REGEX仅适用于一行.我需要多行工作.
例如html:
<li> test </li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我希望REGEX采取一切措施,包括"/ ul>".我的建议是:
'(^.*<li>.*</ul>)'
Run Code Online (Sandbox Code Playgroud)
但它没有用.它甚至可能吗?谢谢.
这取决于您使用的正则表达式方法.
如果使用.NET Regex::Match,则可以使用第三个参数来定义其他regex选项.[System.Text.RegularExpressions.RegexOptions]::Singleline在这里使用:
$html =
@'
<li> test </li>
</ul>
'@
$regex = '(^.*<li>.*\</ul>)'
[regex]::Match($html,$regex,[System.Text.RegularExpressions.RegexOptions]::Singleline).Groups[0].Value
Run Code Online (Sandbox Code Playgroud)
如果你想使用的选择字符串 cmdlet,则必须specifiy的单线选项(?s)您内regex:
$html =
@'
<li> test </li>
</ul>
'@
$regex = '(?s)(^.*<li>.*\</ul>)'
$html | Select-String $regex -AllMatches | Select -Expand Matches | select -expand Value
Run Code Online (Sandbox Code Playgroud)