Sup PSHomies?

So this regular expression thing has it’s advantages.  Like many things practice makes perfect…

In the past whenever I saw regex I’d flat line… There wasn’t really a need to use it, select-string with a dash of  -like, -contains, -split here and there, got me pretty far so why bother eh? Well for one thing regex is for the big boys, much like LDAPFilter on Active Directory cmdlets… Guess it’s time to put on my big boy pants and step my game up! 😛

Jeffrey Hicks recently blogged on converting Text to PSCustomObjects using regex. Anything Jeffrey publishes is golden…

Jeffrey’s pattern is easy enough to follow…

regex]$pattern = '(?<ID>\w+)\s+(?<Chassis>\d)\s+(?<Slot>\d)\s+(?<RAIDID>\w+)\s+(?<Status>\w+)\s+(?<Type>\w+)\s+(?<Media>\w+)'

Here’s a quick breakdown:

  • \w+ : Matches any word character as much as possible (that’s what the + is for)
  • \s+  : Matches any whitespace character as much as possible
  • \d    : Matches any decimal digit

Here a link to regex cheat sheet to explain what the character classes actually do…

Fun fact: I’ve been the proud owner of the Windows PowerShell Pocket reference (first edition, for quite some time I may add) and there is a whole chapter dedicated to regular expressions! Go figure…

The trick is to use capture names in your pattern to store the results. I saw this in the robocopy script by Joakim Svendsen. What I didn’t realize was that you can retrieve the captured names using GetGroupNames() method, nice! Just one thing, the first GroupName is always 0 it seems so just skip that one.

$pattern.GetGroupNames() | select -skip 1

This makes enumerating the names easy! Just head on over to Jeffrey’s blog to see how it’s done… 😉

In the mean time here’s a lil’ something to help you verify that the pattern works… 😉

@'
ID Chassis Slot RAIDID Status Type Media Spare SizeGB
=====================================================
c0d0  0     0    c0r0    Ok     sas  HDD   -    150
c0d0  0     0    c0r1    Ok     sas  HDD   -    150
c0d1  0     1    c0r0    FAILED sas  SDD   -    150
c0d1  0     1    c0r1    FAILED sas  SDD   -    150
'@ | Out-File "$env:TEMP\PatternFile.txt"
 
[regex]$pattern = '(?<ID>\w+)\s+(?<Chassis>\d)\s+(?<Slot>\d)\s+(?<RAIDID>\w+)\s+(?<Status>\w+)\s+(?<Type>\w+)\s+(?<Media>\w+)'
 
 $captureNames = $pattern.GetGroupNames() | Select-Object -skip 1

get-content "$env:TEMP\PatternFile.txt" |
ForEach-Object {
  if($_ -match $pattern){
    $captureResults = @{}
    foreach($captureName in $captureNames){
      $captureResults.Add($captureName,$Matches.$captureName)
    }
    [PSCustomObject]@{
      String = $_
      MatchFound = ($_ -match $pattern)
      regExResults = [PSCustomObject]$captureResults
    }
  }
  Else {
    [PSCustomObject]@{
      String = $_
      MatchFound = ($_ -match $pattern)
      regExResults = ''
    }
  }
} 

wpid-wp-1438336833636.jpg

Hehe… Hope it’s worth something to you

Ttyl,

Urv

Advertisement

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 )

Facebook photo

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

Connecting to %s