Group:  English: General ยป microsoft.public.windows.powershell
Thread: regex global match like Perl m//g

DotNetBag
.NET Development Newsgroups

HTVi
TV Discussion Newsgroups

Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
Rising Antivirus 2006

regex global match like Perl m//g
John Cook 11.07.2007 05:02:01
How do I use PowerShell to find all matches of a regular expression in a
string, analogous to the 'g' option on matches in Perl?
Re: regex global match like Perl m//g
"Kiron" <Kiron[ at ]HighPlainsDrifter.com> 11.07.2007 07:14:53
I'm not sure about Perl's 'm//g' output, no Perl experience :(
To get all Regex matches in a string you can use .Net's Regex.Matches
method:

$string = "x1 c4 c1 x3 c5 c3 x2 x4 c2"
[regex]::matches($string,'c\d')
[regex]::matches($string,'c\d') | format-table -auto
[regex]::matches($string,'c\d') | format-table Value, Index -auto
[regex]::matches($string,'c\d') | format-table value

....also:

[regex]$pattern = 'c\d'
$pattern.matches($string)
$pattern.matches($string) | format-table -auto
$pattern.matches($string) | format-table Value, Index -auto
$pattern.matches($string) | format-table value

http://msdn2.microsoft.com/en-us/library/system.text.regularexpressions.regex.matches.aspx

--
Kiron

Re: regex global match like Perl m//g
John Cook 11.07.2007 11:32:04
Thanks. That's just what I needed to know.

I'll summarize comparing Perl and PowerShell snippets. In Perl you would
fill the array [ at ]cnums with all matches by

$string = "x1 c4 c1 x3 c5 c3 x2 x4 c2";
[ at ]cnums = $string =~ m/c\d/g;

The PowerShell analog would be

$string = "x1 c4 c1 x3 c5 c3 x2 x4 c2"
$cnums = ([regex]::matches($string,'c\d') | %{$_.value})

"Kiron" wrote:

[Quoted Text]
> I'm not sure about Perl's 'm//g' output, no Perl experience :(
> To get all Regex matches in a string you can use .Net's Regex.Matches
> method:
>
> $string = "x1 c4 c1 x3 c5 c3 x2 x4 c2"
> [regex]::matches($string,'c\d')
> [regex]::matches($string,'c\d') | format-table -auto
> [regex]::matches($string,'c\d') | format-table Value, Index -auto
> [regex]::matches($string,'c\d') | format-table value
>
> ...also:
>
> [regex]$pattern = 'c\d'
> $pattern.matches($string)
> $pattern.matches($string) | format-table -auto
> $pattern.matches($string) | format-table Value, Index -auto
> $pattern.matches($string) | format-table value
>
> http://msdn2.microsoft.com/en-us/library/system.text.regularexpressions.regex.matches.aspx
>
> --
> Kiron
>
Re: regex global match like Perl m//g
"Kiron" <Kiron[ at ]HighPlainsDrifter.com> 11.07.2007 12:54:10
Thank you too. Now I know a little bit of Perl :)

--
Kiron
Re: regex global match like Perl m//g
"Keith Hill" <r_keith_hill[ at ]mailhot.moc.no_spam_I> 11.07.2007 15:11:59
"John Cook" <my last name at mdanderson.org> wrote in message
news:5F4171C8-F912-4813-B361-3C0FCD867A5D[ at ]microsoft.com...
[Quoted Text]
> Thanks. That's just what I needed to know.
>
> I'll summarize comparing Perl and PowerShell snippets. In Perl you would
> fill the array [ at ]cnums with all matches by
>
> $string = "x1 c4 c1 x3 c5 c3 x2 x4 c2";
> [ at ]cnums = $string =~ m/c\d/g;
>
> The PowerShell analog would be
>
> $string = "x1 c4 c1 x3 c5 c3 x2 x4 c2"
> $cnums = ([regex]::matches($string,'c\d') | %{$_.value})

FYI I've suggested a -matchall operator be added to PowerShell e.g.:

$cnums = $string -matchall 'c\d' | %{$_[0]}

If you agree you can vote on this feedback item at:

https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=264229&SiteID=99

--
Keith

Re: regex global match like Perl m//g
John Cook 11.07.2007 21:08:01
Thanks. I like the -matchall idea.

The feedback link seems to be broken.

"Keith Hill" wrote:

[Quoted Text]
> "John Cook" <my last name at mdanderson.org> wrote in message
> news:5F4171C8-F912-4813-B361-3C0FCD867A5D[ at ]microsoft.com...
> > Thanks. That's just what I needed to know.
> >
> > I'll summarize comparing Perl and PowerShell snippets. In Perl you would
> > fill the array [ at ]cnums with all matches by
> >
> > $string = "x1 c4 c1 x3 c5 c3 x2 x4 c2";
> > [ at ]cnums = $string =~ m/c\d/g;
> >
> > The PowerShell analog would be
> >
> > $string = "x1 c4 c1 x3 c5 c3 x2 x4 c2"
> > $cnums = ([regex]::matches($string,'c\d') | %{$_.value})
>
> FYI I've suggested a -matchall operator be added to PowerShell e.g.:
>
> $cnums = $string -matchall 'c\d' | %{$_[0]}
>
> If you agree you can vote on this feedback item at:
>
> https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=264229&SiteID=99
>
> --
> Keith
>
Re: regex global match like Perl m//g
"Keith Hill [MVP]" <r_keith_hill[ at ]mailhot.moc_no_spam_I> 11.07.2007 22:33:27
"John Cook" <my last name at mdanderson.org> wrote in message
news:29AF3A0A-FC0E-4E39-964D-18A30090CB5A[ at ]microsoft.com...
[Quoted Text]
> Thanks. I like the -matchall idea.
>
> The feedback link seems to be broken.
>

Try getting a login on http://connect.microsoft.com then find the
WindowsPowerShell product page and select its Feedback link. Search
for -matchall and that should take you to the feedback I submitted.

--
Keith

Re: regex global match like Perl m//g
"Flowering Weeds" <floweringnoweedsno[ at ]hotmail.com> 12.07.2007 22:13:50

"John Cook"

[Quoted Text]
> How do I use PowerShell to find all
> matches of a regular expression
> in a string, analogous to the 'g' option
> on matches in Perl?

FYI

When one needs to parse almost any
thing in Windows perhaps Microsoft's
Log Parser 2.2

PS> $string | LogParser.exe "SELECT text FROM STDIN WHERE text LIKE
'c%'" -i:tex
tword -stats:off
Text
----
c4
c1
c5
c3
c2
PS>

There is also a way to add
in other ways. At one time
there was even a regex C#
add-in for LogParser too!

Just another way!




Re: regex global match like Perl m//g
Hal Rottenberg <halr9000[ at ]gmail.com> 13.07.2007 17:42:11
On Jul 12, 6:13 pm, "Flowering Weeds" <floweringnoweed...[ at ]hotmail.com>
wrote:
[Quoted Text]
> When one needs to parse almost any
> thing in Windows perhaps Microsoft's
> Log Parser 2.2

Seriously dude, do you get a commission for every download or
something? :)

Re: regex global match like Perl m//g
"Keith Hill [MVP]" <r_keith_hill[ at ]mailhot.moc_no_spam_I> 13.07.2007 20:03:46
"Hal Rottenberg" <halr9000[ at ]gmail.com> wrote in message
news:1184348531.831584.277990[ at ]57g2000hsv.googlegroups.com...
[Quoted Text]
> On Jul 12, 6:13 pm, "Flowering Weeds" <floweringnoweed...[ at ]hotmail.com>
> wrote:
>> When one needs to parse almost any
>> thing in Windows perhaps Microsoft's
>> Log Parser 2.2
>
> Seriously dude, do you get a commission for every download or
> something? :)

LOL! I was wondering the same thing. :-)

--
Keith

Re: regex global match like Perl m//g
"Brandon Shell" <tshell.mask[ at ]mk.gmail.com> 14.07.2007 00:14:38
ditto
"Keith Hill [MVP]" <r_keith_hill[ at ]mailhot.moc_no_spam_I> wrote in message
news:26A50AA5-2302-480C-94CC-85498B2339A6[ at ]microsoft.com...
[Quoted Text]
> "Hal Rottenberg" <halr9000[ at ]gmail.com> wrote in message
> news:1184348531.831584.277990[ at ]57g2000hsv.googlegroups.com...
>> On Jul 12, 6:13 pm, "Flowering Weeds" <floweringnoweed...[ at ]hotmail.com>
>> wrote:
>>> When one needs to parse almost any
>>> thing in Windows perhaps Microsoft's
>>> Log Parser 2.2
>>
>> Seriously dude, do you get a commission for every download or
>> something? :)
>
> LOL! I was wondering the same thing. :-)
>
> --
> Keith

Home | Search | Terms | Imprint | Contact
Newsgroups Reader - provided by WiredBox.Net