6

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

For example html:

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

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

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

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

charles
  • 277
  • 2
  • 7
  • 14
  • Possible duplicate of http://stackoverflow.com/questions/21911293/multiline-regex-in-powershell – Igal S. Aug 10 '15 at 07:17

2 Answers2

11

It depends on what regex method you are using.

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

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
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
1

Using a multi-line single-line regex, with -match:

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


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

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

<li> test </li>
</ul>
mjolinor
  • 66,130
  • 7
  • 114
  • 135