My Technical Notes

Friday, 16 June 2017

PowerShell: Extract text using `-replace`

Given the following string:


Warning AD1001 The assembly C:\USERS\TH203\APPDATA\LOCAL\MICROSOFT\VISUALSTUDIO\15.0_D3DC1757\EXTENSIONS\OQM3T4SF.ZWU\Microsoft.NetCore.VisualBasic.Analyzers.dll does not contain any analyzers.   1 Active

to extract only the DLL names, use the `-replace` operator with a regular expression that captures the DLL name and the replacement text referring to the captured group's value, `$1`:


$str -replace '.*\\([^\\]*\.dll).*','$1'

Ensure that you have `.*` at the beginning and end of the regex so that it matches the full content of the input string, otherwise the output will contain unmatched text before and after the data you have pulled out.

No comments: