Avatar of 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) {
		$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?

Avatar of undefined
Last Comment

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Ah ha!  Thank you.  I'm getting spoiled from all my data normally being nicely formatted for me.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes