regex - How to grab XML section in text file? -
i have string has section in xml format. how can grab section using powershell? below have tried far, not working. appreciated.
$text = "2015-07-09 16:19:12,900|info execute 2015-07-09 16:19:12,978|info stop 2015-07-09 16:19:12,978|info start ?<config> <name>shsjsjsjsl</name> <id>0</id> <status>passed</status> </config> 2015-07-09 16:19:13,461|info end" $xmldata = $text -match "\?<config>(.*?)</config>" $xmldata
you should include <config> tags in $xmldata variable, because xml documents must have single root. , since xml section spans multiple lines must make regular expression include newlines well, e.g. using single line modifier ((?s)):
if ($text -match '(?s)<config>.*?</config>') { $xmldata = $matches[0] } or matching whitespace (\s) , non-whitespace (\s):
if ($text -match '<config>[\s\s]*?</config>') { $xmldata = $matches[0] }
Comments
Post a Comment