Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2562

How to return part of a string based on a regular expression as a string and not an multi-value object

$
0
0

I have an object that returns two fields that I want to present as output, but the second field needs to be modified to drop part of the data. The object looks like this:

Parent             FileName

-------------       ----------------

NAME1             Garbage I don't want [WHAT I WANT #1] Garbage I don't want
NAME2             Garbage I don't want [WHAT I WANT #2]
NAME3             [WHAT I WANT #3] Gargabe I don't want

Everything I want is within the square brackets. I don't even care if the brackets are presented. I just want to drop the random waste around the square brackets.

This is what I have been trying to work with:

$MyObject |    select `
            @{N="ServerName";E={$_.Parent},`
            @{N="FileName";E={([REGEX]::Matches($_.FileName, "\[.{0,}\]")).value}}
        }
}

What I am trying to get out of this is the following:

Name1                 [WHAT I WANT #1]
Name2                 [WHAT I WANT #2]
Name3                 [WHAT I WANT #3]

That of course does not work because the .value portion of the statement returns an object instead of a string (presumably because more than one answer could be returned under a regular expression. So how do I fix that. There will only be one instance of the square brackets, and even if there where more, I would be pretty happy to just get the first instance and stop there. I just don't see a way to do it...



Viewing all articles
Browse latest Browse all 2562

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>