matching many numbers

Welcome Forums General PowerShell Q&A matching many numbers

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years ago.

  • Author
  • #5549

    Points: 0
    Rank: Member

    by stocksp at 2013-02-13 21:09:23

    "1b4c6f8" -match ("\d")

    I expected 4 matches (as I do get in 'regular' .net) but $matches only returns one.

    What is the syntax for multiple matches?



    by vstarmanv at 2013-02-13 21:27:58

    do you want this?

    ([char[]]"1b4c6f8" -match "\d").Count

    by stocksp at 2013-02-13 23:05:14

    No, I need the values. My actual expression is more complex. I'm scrapping an HTML document.
    I want a collection of all the matches.
    The simple example should produce a collection with 4 numbers

    by Klaas at 2013-02-14 01:04:39

    I found this tip from Keith Hill. It's from 2007 so there may be another way by now, but this works. ([regex]'\d').matches("a1b2c3d4e5") | Foreach { $_.value }

    by mjolinor at 2013-02-14 03:38:22

    That first solution posted should work, if you just get rid of the ().count:

    [char[]]"1b4c6f8" -match "\d"

    by stocksp at 2013-02-14 08:56:15

    Thanks a million!
    I have a 'page' of text and I'm looking for a list of 'WINNER OF THE XXX....' embedded in HTML
    Smatches[0] contains the page.

    ([regex]'(WINNER OF.*(?=))').matches($matches[0]) | Foreach { $_.value }

    This works perfect. Is there a solution that uses -match?


    by Klaas at 2013-02-14 09:11:10

    I don't think so.
    This is a part of the about_comparison_operators:

    Description: Matches a string using regular expressions.
    When the input is scalar, it populates the
    $Matches automatic variable.

    PS C:\> "Sunday" -Match "sun"

    PS C:\> $matches
    Name Value
    —- —–
    0 Sun

    PS C:\> "Sunday", "Monday" -Match "sun"

The topic ‘matching many numbers’ is closed to new replies.