Return count of all matches of a regular expression

I was required to produce the count of a particular string within a xmla file and output the count as a numeric to a variable.

First of all I introduced the content of my xmla file as a powershell variable unsing the Get-Content cmdlet

$Content = Get-Content “C:\My Folder\My File.xmla ”

Once I had the content of my text file as a variable I then used regular expression to match the search filter to the content variable and return the number of matches (.count).

$Count = [regex]::matches($Content,”My Search Filter”).count

Now by running the write-output cmdlet with the Content variable the numeric value is displayed

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s