Fun with Regular Expressions

A while back a friend of mine mentioned that he could not find a regular expression that was capable of parsing Windows Performance counter strings. He said that it couldn’t be done with regex alone and he had written a lot of code to manually parse the strings. That sounded like a challenge to me. I had recently been working on a project where I needed regular expressions to find and clean up text that I was extracting from a large database. I had spent a lot of time learning what I could about regex to make the job easier. Along the way, I found a great tool called Expresso.

expresso screenshot

Expresso showing the parsed results of this regex

Expresso is a power tool for developing and testing regular expressions. In just a few minutes I was able to create a regex that fit the bill. Since he was writing code in PowerShell to process these performance counters I sent him this proof of concept.

$ctrs = (
  '\\IDCWEB1\Processor(_Total)\% Processor Time',
  '\Paging File(\??\C:\pagefile.sys)\% Usage Peak',
  '\MSSQL$SQLServer:Memory Manager\Total Server Memory (KB)',
  '\\BLACKVISE\Paging File(\??\C:\pagefile.sys)\% Usage Peak',
  '\Category(Instance(x))\Counter (x)',
  '\SQLServer:Latches\Latch Waits/sec (ms)'
)

$pattern = '(?<srv>\\\\[^\\]*)?\\(?<obj>[^\(^\)]*)(\((?<inst>.*(\(.*\))?)\))?\\(?<ctr>.*\s?(\(.*\))?)'

foreach ($ctr in $ctrs) {
  if ($ctr -match $pattern) {
    "Server = " + $matches["srv"]
    "Object = " + $matches["obj"]
    "Instance = " + $matches["inst"]
    "Counter = " + $matches["ctr"]
    ""
  }
}

Here is the output :

Server = \\IDCWEB1
Object = Processor
Instance = _Total
Counter = % Processor Time

Server =
Object = Paging File
Instance = \??\C:\pagefile.sys
Counter = % Usage Peak

Server =
Object = MSSQL$SQLServer:Memory Manager
Instance =
Counter = Total Server Memory (KB)

Server = \\BLACKVISE
Object = Paging File
Instance = \??\C:\pagefile.sys
Counter = % Usage Peak

Server =
Object = Category
Instance = Instance(x)
Counter = Counter (x)

Server =
Object = SQLServer:Latches
Instance =
Counter = Latch Waits/sec (ms)

By the way, the PowerShell script he was writing was part of PAL. Check out Clint’s incredible performance analysis tool.

I am an experienced IT technologist specializing in optimizing user experiences, providing best-in-class support and developing creative solutions. I script therefore I am. I build tools to improve troubleshooting and gather supporting data.

Tagged with: , ,
Posted in Scripting

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

sean on it
Categories
Follow Sean on IT on WordPress.com
Blog Stats
  • 65,943 hits
Mike F Robbins

Scripting | Automation | Efficiency

%d bloggers like this: