We help IT Professionals succeed at work.
Get Started

Parsing Select-String pattern matches

Souladin
Souladin asked
on
1,019 Views
Last Modified: 2014-05-06
(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?
Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE