1

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!

3 Answers 3

2

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:

"([^"]*)"
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. Indeed, it appears once I corrected my reg-ex, I was good to go.
The question mark in this context tells the asterisk to not be greedy.
If this were Perl, you would be correct. However, like I thought, Powershell sees a question mark as a match for 0 or 1 of the previous character. See the doc here.
It determines if a quantifier is greedy because it is placed after a quantifier. If it were not placed after a quantifier, then yes, it would make the preceding token optional.
Interesting, I haven't seen anything in docs as far as dual functionality of the character, do you have a doc you can point to? I've had to find annoying workarounds because I just assumed that it doesn't control greediness, that would be great if you're correct.
1

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 {

1 Comment

Thank you. It appears that, combined with a change in the reg-ex, brought me to where I needed to go.
0

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}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.