How would I find a multiline pattern in files, such as the contents of an XML node, using Powershell?
i.e. if I were looking for the word "green" within the deviceDescription
node, but the XML node text may span multiple lines, this doesn't work:
dir -r -i *.xml | select-string -Pattern "<deviceDescription>.*green.*</deviceDescription>"
First of all, if is xml, extract the device description string treating it as such and then match for the string you want, in this case, green.
$x = [xml] (gc .\test.xml)
$x.deviceDescription -match "green"
If you don't want to go that way, you will have to use the ?s
- singleline or dotall flag which makes * match newlines:
$x = [IO.File]::ReadAllText("C:\test.xml")
$x -match "(?s)<deviceDescription>.*green.*</deviceDescription>"
Note that you probably want to use .*?
or this may span across multiple deviceDescription tags. Edge cases like this are reasons why you should avoid using regex for such things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With