Author Posts

January 1, 2012 at 12:00 am

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?

thanks!

P:)

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 }
http://rkeithhill.wordpress.com/2007/09/29/effective-powershell-item-9-regular-expressions-one-of-the-power-tools-in-powershell/

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?

P:)

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

I don't think so.
This is a part of the about_comparison_operators:
[quote] -Match
Description: Matches a string using regular expressions.
When the input is scalar, it populates the
$Matches automatic variable.
Example:

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

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

PS C:\> "Sunday", "Monday" -Match "sun"
Sunday[/quote]