[PS] User Guide inconsisent on -match operator

Discussion in 'Scripting' started by forestial, Apr 28, 2006.

  1. forestial

    forestial Guest

    In the Windows PowerShell User Guide there are two statements about the
    -match operator that are contradictory:

    On page 78, it says
    -Match uses the .NET regular expression object to determine whether a
    string contains a pattern. If a match is found during the operation, the
    automatic hashtable variable $matches will contain the matching text.

    On page 83, a Note says
    The -match operator captures only the first match found. If you want to
    ensure that you capture all the matches, you will need to use regular
    expression objects and methods directly.

    From what I have tested, $matches does contain all of the matches, not just
    the first one. So I think page 83 is incorrect.
    forestial, Apr 28, 2006
    1. Advertisements

  2. I think page 83 refers to the following:

    PS>"1 2 3" -match "\d"

    Name Value
    ---- -----
    0 1


    Notice that only the first number was captured even though the regular
    expression will also match 2 and 3. There's no way for me to say, get the
    next match on the same string. I would have to use the regex classes for

    - Marcel
    Marcel J. Ortiz Soto [MSFT], Apr 28, 2006
    1. Advertisements

  3. forestial

    forestial Guest

    Ah. Perhaps I was reading something into p83 that wasn't there.

    However I did
    MSH:407> "1 2 3" -match "(\d) (\d) (\d)"

    MSH:408> $matches

    Name Value
    ---- -----
    3 3
    2 2
    1 1
    0 1 2 3

    so it is possible to get any number of matches (or match groups as I think
    Perl would call them).

    This behavior is very handy compared to wrestling with the full regex
    matching stuff but it doesn't seem to be documented in the User Manual.
    Perhaps I am skating on thin ice by relying on it?
    forestial, Apr 28, 2006
  4. Inline.

    That probably means we need to explain it better. :)

    Yes, as long as you know how many groups there are going to be you're fine.
    The problem is when you don't know how many groups there are going to be.

    Nah, you're fine. This is just using the .net framework regular expression
    classes so you can rely on anything in there. Here's a link to information
    on the framework regular expression capabilities.

    Please open a doc bug if you think the document should have a link to this

    - Marcel
    Marcel J. Ortiz Soto [MSFT], Apr 28, 2006
  5. forestial

    forestial Guest

    forestial, May 2, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.