Regex not working in Powershell -
i have powershell file myfile.ps1
:
function do-it { $items = # command returns collection of strings like: # element 12657 - <description trext> # element 12656 - <description trext> # element 12655 - <description trext> # ... $pattern = 'element\s(\d*).*'; foreach ($item in $items) { $res = $item -match $pattern; $len = $matches.length; $id = $matches[0]; write-output "$len $id"; } }
the problem output is:
1 element 12657 - <description trext> 1 element 12656 - <description trext> 1 element 12655 - <description trext> ...
so no match found. however, if execute cmd
, results.
what doing wrong? need escape something? thanks
take , see it's doing manually:
ps u:\> $item = 'element 12657 - <description trext>' ps u:\> $pattern = 'element\s(\d*).*' ps u:\> $matches name value ---- ----- 1 12657 0 element 12657 - <description trext>
i try $id = $matches[1];
.
Comments
Post a Comment