arrays - PowerShell regex to extract SID from filename -
i have array $vhdlist contents similar following filenames:
uvhd-s-1-5-21-8746256374-654813465-374012747-4533.vhdx uvhd-s-1-5-21-8746256374-654813465-374012747-6175.vhdx uvhd-s-1-5-21-8746256374-654813465-374012747-8147.vhdx uvhd-template.vhdx i want use regex , left array containing sid portion of filenames.
i using following:
$sids = foreach ($file in $vhdlist) { [regex]::split($file, '^uvhd-(?:([(\d)(\w)-]+)).vhdx$') } there 2 problems this: in resulting array there 3 blank lines every sid; , "template" filename matches (the resulting line in output "template"). how can array of sids output , not include "template" line?
you seem want filter list down filenames contain sid. filtering done where-object (where short); don't need loop.
an sid described "s- , bunch of digits , dashes" simple case. leaves ^uvhd-s-[\d-]*\.vhdx$ filename.
in combination get:
$vhdlist | { $_ -match "^uvhd-s-[\d-]*\.vhdx$" } when don't have array of strings, array of files, use them directly.
dir c:\some\folder | { $_.name -match "^uvhd-s-[\d-]*\.vhdx$" } or, possibly can make simple as:
dir c:\some\folder\uvhd-s-*.vhdx edit
extracting sids list of strings can thought combined transformation (for each element, extract sid) , filter (remove non-matches) operation.
powershell's foreach-object cmdlet (foreach short) works map() in other languages. takes every input element , returns new value. in effect transforms list of input elements output elements. -replace operator can extract sids way.
$vhdlist | foreach { $_ -replace ^(?:uvhd-(s-[\d-]*)\.vhdx|.*)$,"`$1" } | { $_ -gt "" } the regex back-reference .net languages $1. $ special character in powershell strings, needs escaped, except when there no ambiguity. backtick ps escape character. can escape $ in regex well, there it's not necessary.
as final step use where remove empty strings (i.e. non-matches). doing way around means need apply regex once, instead of 2 times when filtering first , replacing second.
powershell operators can work on lists directly. above shortened:
$vhdlist -replace "^uvhd-(s-[\d-]*)\.vhdx$","`$1" | { $_ -gt "" } the shorter version works on lists of actual strings or objects produce right thing when .tostring() called on them.
regex breakdown:
^ # start-of-string anchor (?: # begin non-capturing group (either...) uvhd- # 'uvhd-' ( # begin group 1 s-[\d-]* # 's-' , many digits , dashes ) # end group 1 \.vhdx # '.vhdx' | # ...or... .* # else ) # end non-capturing group $ # end-of-string anchor
Comments
Post a Comment