Skip to content

Instantly share code, notes, and snippets.

@stelf
Last active September 30, 2021 16:49
Show Gist options
  • Save stelf/0fb983dfe4e7f3925d0d5c6d73b5cf52 to your computer and use it in GitHub Desktop.
Save stelf/0fb983dfe4e7f3925d0d5c6d73b5cf52 to your computer and use it in GitHub Desktop.
dynamic regex switch (PowerShell)
[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