How to filter a select-string to only display the found word with wildcard

I am using

Select-String -Path "some\path\smthing.xml" -pattern "smthing*"

which displays the whole line that contains this pattern.
The problem is that there are random numbers after smthing and are always different so I can only use wildcard.

How to filter that result to display only the matching string, ie – smthing44326 instead of the whole line of the file?

>Solution :

First you need to use a pattern to match specifically smthing followed by one or more numbers (\d+), then once you have the right pattern you need to reference the .Matches.Value property from the objects outputted by Select-String:

Select-String -Path some\path\smthing.xml -Pattern 'smething\d+' | 
    ForEach-Object { $_.Matches.Value }

Leave a Reply