Last active
September 30, 2021 16:49
-
-
Save stelf/0fb983dfe4e7f3925d0d5c6d73b5cf52 to your computer and use it in GitHub Desktop.
dynamic regex switch (PowerShell)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[string[]] $a = ( | |
" asd", | |
" asd ", | |
" bsd", | |
" XXX", | |
"10000" | |
) | |
[string] $spacer = "" | |
$a | ForEach-Object { | |
switch -Regex ($_) { | |
"^\s+" { | |
($spacer.Length -lt $Matches[0].Length) -and ( $spacer = $Matches[0] ) | out-null | |
} | |
"^$spacer" { | |
Write-Host Spacer with length $($spacer.Length) is present in [$_] | |
} | |
default { | |
Write-Host Spacer with length $($spacer.Length) is NOT present in [$_] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment