![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||