Reg
Reg

Reputation: 13

Powershell Regex Complex Pattern (XML)

I have a string in an XML which I am trying to retrun to a varable (below).

<XMLElement><![CDATA[TEXT - I - FIND - INTERESTING]]></XMLElement>

My problem is that I have no knowledge of regex and I am finding it difficult to understand how to convert my special characters into a "string" which is not interpreted as a regex charater.

The text between the square brackets can be any length and any charachter; this is what I would like to save to my variable e.g. "TEXT - I - FIND - INTERESTING".

I am also wondering if this is actually the best approach to this problem.

Any support would be greatly appreciated.

Upvotes: 1

Views: 351

Answers (2)

js2010
js2010

Reputation: 27491

The text() function is nice. Here's another way. Even if the node isn't at the top, this should work.

get-content -raw file.xml | select-xml '//XMLElement' | 
  % { $_.node.'#cdata-section' }

TEXT - I - FIND - INTERESTING

Note that tags would be ignored in a cdata section. Example:

'<script>
  <![CDATA[<message>Welcome to TutorialsPoint</message>]]>
</script>' | select-xml '/script' | % { $_.node.'#cdata-section' }

<message>Welcome to TutorialsPoint</message>

Upvotes: 0

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174690

Don't use regex for XML!

Working with XML in PowerShell has the advantage that we can use XPath to search and navigate a document. In your case I would use the text() selector to resolve the inner value of the CDATA section:

$xml = [xml]@'
<root>
    <XMLElement><![CDATA[TEXT - I - FIND - INTERESTING]]></XMLElement>
</root>
'@

$interestingString = $xml.SelectSingleNode('//XMLElement/text()').Value

//XMLElement means "find and <XMLElement> anywhere"

Upvotes: 3

Related Questions