2

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?

3
  • 1
    Hint: pipe the output to Get-Member Commented May 17, 2016 at 16:56
  • Oh, I need to access $results.Matches. Thanks! :) Commented May 17, 2016 at 17:05
  • Pipe the Matches property to Get-Member and you should be far enough down the rabbit hole :-) Commented May 17, 2016 at 17:07

1 Answer 1

1

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! ;)

Sign up to request clarification or add additional context in comments.

1 Comment

"You can accept your own answer tomorrow" I'll do it as soon as it's possible :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.