sevi
sevi

Reputation: 480

RegEx not matching when using Select String

I've verified that my regex is correct with this code:

#this is the string where I'm trying to extract everything within the []
$text = "MS14-012[2925418],MS14-029[2953522;2961851]"
$text -match "\[(.*?)\]"
$matches[1]

Output:

True
2925418

I'd like to use Select-String to get my result, like this for example:

$result = $text| Select-String -Pattern $regex

Output:

MS14-012[2925418],MS14-029[2953522;2961851]

What else I've tried:

$result = Select-String -Pattern $regex -InputObject $text
$result = Select-String -Pattern ([regex]::Escape("\[(.*?)\]")) -InputObject $text

And some more variations as well as different kinds of " and ' around the regex and so on. I'm really out of ideas...

Can anyone please tell me why the regex is not matching when I'm using Select-String?

Upvotes: 2

Views: 1350

Answers (1)

sevi
sevi

Reputation: 480

After piping the output to Get-Member I noticed that Select-String returns a MatchInfo object and that I needed to access the MatchInfo.Matches property to get the result. Thanks to Mathias R. Jessen for giving me the hint! ;)

Upvotes: 1

Related Questions