Windows Vista Forums

RC2 and something weird...
  1. #1


    PSme Guest

    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

  2. #2


    klumsy@xtra.co.nz Guest

    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

  3. #3


    dreeschkind Guest

    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

RC2 and something weird... problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird - Very weird annoying problem GeorGeZ General Discussion 6 23 Aug 2009
this is a weird one...maybe someone can help... turbovf1s Network & Sharing 4 16 Mar 2009
Weird!! Phil Vista mail 8 11 Aug 2008
PC does weird bt4ever General Discussion 0 25 Feb 2008
weird bug David Sherman Vista installation & setup 2 30 Oct 2006