Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - RC2 and something weird...

Reply
 
Old 10-26-2006   #1 (permalink)
PSme


 
 

RC2 and something weird...

Im no expert when it comes to powershell but since i updated to RC2 i noticed
that the .count and regex things have been very strange.

In the example below ive got simple regular expression match inside the
function. For some reason it gives different results compared to having the
same regex line but outside of the function.

Again, this exact same thing worked just fine in RC1. Not in RC2.

function regexmatch($instr1, $instr2){
return $([regex]$instr1).Matches($instr2)
}

# count should be one, but nothing...
(regexmatch "this" "this is a test").count

# with two and it works.
(regexmatch "is" "this is a test").count

# one again with measure-object, works.
(regexmatch "this" "this is a test" | measure-object).count

# one match found and this doesnt work... exception error.
(regexmatch "this" "this is a test")[0]

# with two... and it works.
#(regexmatch "is" "this is a test")[0]


# this is the "exact" same thing as the first one but not using through the
function...and it works.
#$([regex]"this").Matches("this is a test").count

# accessing the first one works in this case...unlike above.
$([regex]"this").Matches("this is a test")[0]

I just dont get it. Any ideas?

My System SpecsSystem Spec
Old 10-26-2006   #2 (permalink)
klumsy@xtra.co.nz


 
 

Re: RC2 and something weird...

powershell will strip a collection/array every chance it gets.. and
that usually happens at the pipeline boundary, but also expressions
like $() and functions effectively work the same way...

so i did this

$a = ([regex]"this").Matches("this is a test")

and i see that it is a matchcollection

# count should be one, but nothing...
$b = (regexmatch "this" "this is a test")

while is stripping the one itemed collection to just the match, and the
type returned in the actual match. so to maintain the array/collection
we can adjust your function with the , which will force it not to be
stripped. (or rather wrap it in an array, so that only that wrapped
array gets stripped)

function regexmatch($instr1, $instr2){
,$([regex]$instr1).Matches($instr2)
}

will do the trick..

My System SpecsSystem Spec
Old 10-26-2006   #3 (permalink)
dreeschkind


 
 

Re: RC2 and something weird...

"klumsy@xtra.co.nz" wrote:

> powershell will strip a collection/array every chance it gets..


Can somebody confirm whether there realy has been a change in this behaviour
from RC1 to RC2? I thought this bahaviour was much older. I'm confused now.

--
greetings
dreeschkind
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Weird - Very weird annoying problem General Discussion
this is a weird one...maybe someone can help... Network & Sharing
Ok, this is REALLY weird. Live Mail
Weird Chillout Room
Weird!! Vista mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46