簡體   English   中英

REGEX多行 - powershell

[英]REGEX for multiple lines - powershell

我在powershell中使用正則表達式有一點問題。 我的REGEX僅適用於一行。 我需要多行工作。

例如html:

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

我希望REGEX采取一切措施,包括“/ ul>”。 我的建議是:

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

但它沒有用。 它甚至可能嗎? 謝謝。

取決於您使用的正則表達式方法。

如果您使用.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

如果要使用Select-String cmdlet,則必須在regex指定單行選項(?s)

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

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

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

使用多行單行正則表達式,使用-match

$string = @'
notmached
<li> test </li>
</ul>
notmatched
'@


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

$string -match $regex > $null
$matches[1]

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM