Link to home
Start Free TrialLog in
Avatar of Souladin
Souladin

asked on

Parsing Select-String pattern matches

(This is a follow up to the question that I posted here)

While the regex works great, this is the first time I've worked with pulling matches from a text file this way (normally I get CSV or Excel files) and I'm getting lost parsing through them all and storing the values into variables.  For reference, here is an example of what is in each text file being queried:

<option value="1992560">LASTNAME, FIRSTNAME - &#x5b;firstname.lastname&#x40;domain.com&#x5d;</option>
								
<option value="1934672">TEST, TESTTODAY - &#x5b;testtoday&#x40;domain.com&#x5d;</option>
								
<option value="1912146">TEST, TEST1 - &#x5b;test1234&#x40;gmail.com&#x5d;</option>
								
<option value="1900934">TEST, TEST2 - &#x5b;ctest119&#x40;test.com&#x5d;</option>
								
<option value="1900933">TEST, TEST3 - &#x5b;ctest117&#x40;test.com&#x5d;</option>
								
<option value="1958349">TEST, RTO - &#x5b;generic100&#x40;domain.com&#x5d;</option>

Open in new window


Here is the test script I'm using to parse through one file.  

$filePath = "\\SERVER01\NetShare\IT\Infrastructure\Desktop\Audit\UserLists\env\Beta.txt"
$regex = "<option value=""(\d*)"">([\w ]*), ([\w ]*)- &#x5b;(.*)&#x40;(.*)&#x5d;"
$fileContent = Select-String -Path $filePath -Pattern $regex -AllMatches
ForEach ($groups in $fileContent.Matches) {
	ForEach ($group in $groups.Groups) {
		$personID
		$fullName
		$emailAddress
		$SQLQuery = "INSERT INTO [#env$envName] VALUES ('$personID','$fullName','$emailAddress')"
		Write-Output $SQLQuery
	}
}

Open in new window


Where I'm getting lost is how to get the various values that are in the .Matches.Groups into the $personID, $fullName, and $emailAddress variables.  Or am I going about parsing $fileContent all wrong?
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Souladin
Souladin

ASKER

Ah ha!  Thank you.  I'm getting spoiled from all my data normally being nicely formatted for me.