Search code examples
powershellreplacetrim

Using either Trim or Replace in PowerShell to clean up anything not contained in quotation marks


I'm trying to write a script that that will remove everything except text contained in quotation marks from a result-set generated by a SQL query. Not sure whether trim or -replace will do this.

Here is a sampling of the result-set:

a:5:{s:3:"Client Service";a:4:{s:15:"Client Training";b:0;s:11:"Payer Error";

I would like it to end up looking like this:

Client Service
Client Training
Payer Error

I've tried everything I know to do in my limited PowerShell and RegEx familiarity and still haven't been able to figure out a good solution.

Any help would be greatly appreciated.


Solution

  • Here's a version that uses Regex to capture the strings including their quotes in to an array and then removes the quote marks with -replace:

    $text = 'a:5:{s:3:"Client Service";a:4:{s:15:"Client Training";b:0;s:11:"Payer Error";'
    
    ([regex]::matches($Text, '(")(.*?)(")')).value -replace '"'
    

    There's without a doubt a regex to get the strings without their quotes in the first place but i'm a bit of a regex novice.