用于多行的 REGEX - powershell [英] REGEX for multiple lines - powershell

查看:42
本文介绍了用于多行的 REGEX - powershell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 powershell 中使用正则表达式有点问题.我的 REGEX 仅适用于一行.我需要在多条线上工作.

I have a little problem with regex in powershell. My REGEX works only for one line. I need to work on multiple lines.

例如html:

<li> test </li>
</ul>

我希望 REGEX 处理所有内容,包括/ul>".我的建议是:

I want REGEX took everything, including "/ul>". My suggestion is:

'(^.*<li>.*</ul>)' 

但是没用.甚至有可能吗?谢谢.

But it donesn't work. Is it even possible? Thanks.

推荐答案

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

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

这篇关于用于多行的 REGEX - powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆