Reputation: 2086
Using this reg-ex tester: http://myregextester.com/index.php Indicates my regex should work: Regex:
{name:"(\w*?)", rank:([\d]+)},
Sample Data to capture:
{name:"AARON", rank:77},
{name:"ABBEY", rank:1583},
Here's the powershell script I'm attempting to run , to parse json-like data into a powershell grid.
$regex = '{name:"(\w*?)", rank:([\d]+)},'
(Select-String -Path EmailDomains.as -Pattern $regex -AllMatches).matches |foreach {
$obj = New-Object psobject
$obj |Add-Member -MemberType NoteProperty -Name Rank -Value $_.groups[1].value
$obj |Add-Member -MemberType NoteProperty -Name Name -Value $_.groups[0].value
$obj
} |Out-GridView -Title "Test"
The reg-ex never seems to return values (I'm guessing its a MS regex versus Perl regex mixup, but I can't identify), so I'm not sure what the issue could be. Any help is appreciated!
Upvotes: 1
Views: 1080
Reputation: 2847
The question mark often has different functionality in different environments (in this one, I think it means "match the preceding character 0 or 1 times"). I doubt that it is the same as Perl's. Instead of
"(\w*?)"
Try:
"([^"]*)"
Upvotes: 2
Reputation: 29450
Your expression:
(Select-String -Path EmailDomains.as -Pattern $regex -AllMatches)
returns an array of MatchInfo objects. The array itself does not have a Matches property.
What you have to do is expand the Matches property using the Slect-Object commandlet, then pass that along your pipeline:
Select-String -Path EmailDomains.as -Pattern $regex -AllMatches | select-object -expand Matches | foreach {
Upvotes: 1
Reputation: 12433
I don't think your regex is the problem. Matches is a property on each of the objects returned by Select-Object, not on the collection of objects returned.
$regex = '{name:"(\w*?)", rank:([\d]+)},'
$matches = (Select-String -Path .\a.txt -Pattern $regex)
$matches | Select -ExpandProperty Matches | Select @{n="Name";e={$_.Groups[1].Value}}, @{n="Rank";e={$_.Groups[2].Value}}
Upvotes: 0