Words: 725Time to read: ~4 minutes
Intro
Latest reflection
Processing database: [DBName] @ 2017-11-13 05:07:18 [SQLSTATE 01000]Processing database: [Database] @ 2017-11-13 05:27:39 [SQLSTATE 01000]
Including the WordPress format because WordPress doesn’t want to open Gists anymore, for some reason :/
$Database = 'Processing database: [DBName] @ 2017-11-13 05:07:18 [SQLSTATE 01000]', 'Processing database: [Database] @ 2017-11-13 05:27:39 [SQLSTATE 01000]' foreach ($Db in $Database) { [PSCustomObject]@{ OriginalLine = $Db IndexOfOpeningSquare = $Db.IndexOf('[') IndexOfClosingSquare = $Db.IndexOf(']') } } #Substrings #1 foreach ($Db in $Database) { $Db.Substring($Db.IndexOf('[') + 1) } #Finished foreach ($Db in $Database) { $Db.Substring($Db.IndexOf('[') + 1, ($Db.IndexOf(']') - $Db.IndexOf('[')) - 1) } #split nested arrays #1 foreach ($Db in $Database) { ($Db -split '\[') } #2 foreach ($Db in $Database) { ($Db -split '\[')[1] } #3 foreach ($Db in $Database) { ($Db -split '\[')[1] -split '\]' } #Finished foreach ($Db in $Database) { (($Db -split '\[')[1] -split '\]')[0] } #first multiple delimiter split #1 foreach ($Db in $Database) { ($Db -split {$_ -eq '[' -or $_ -eq ']'}) } #Finished foreach ($Db in $Database) { ($Db -split {$_ -eq '[' -or $_ -eq ']'})[1] } #Final method #1 foreach ($Db in $Database) { ($Db -split '[\[\]]') } #Finished foreach ($Db in $Database) { ($Db -split '[\[\]]')[1] }
Let’s get some basic information about our strings, shall we?

Substring
Coming from a SQL Server background, you can bet that I tried to use SUBSTRING
for this!
First, let’s see if we can get the start of the database name?

And now, we can get the rest!

-Split
After a quick glance at Get-Help about_Split
I moved onto using the -split
operator
First, what happens when we split our string on ‘[‘?

Can we split that string on ‘]’ to get the rest?

And we only want the first result for each so we filter it to that!

Scriptblock?
Slow your horses Shane, read about_Split
again…
<String> -Split {<ScriptBlock>} [,<Max-substrings>]
Wait, it takes a script block? No way! Could this mean that we can split on two different delimiters?

Apply filter and…

Ranges!
Can we go further? Scriptblocks are great but can we do better? Remember with -Split
we had to escape the ‘[‘ because of regex? Well let’s use an extremely basic form of regex. You’ve even seen it with SQL Server!
Moving on to next reflection
Let’s just compare the first script with the last script, shall we?
#Finished foreach ($Db in $Database) { $Db.Substring($Db.IndexOf('[') + 1, ($Db.IndexOf(']') - $Db.IndexOf('[')) - 1) } #Finished foreach ($Db in $Database) { ($Db -split '[\[\]]')[1] }
Personally I prefer the second one, brevity wins out overall, plus reading this it just seems easier to understand.
Okay…we’re taking the index of ‘[‘ adding 1…what?…in the string…but only until the index of ‘[‘…what?…minus the…what? the original index?!…taking away 1??? Then why are we adding it!!!
Versus
Okay…what the hell?…oh it’s a range, I’ve seen them with LIKE in T-SQL…splitting on ‘[‘ and ‘]’. Alright!
Reflection done. Time to improve.