Last active
September 26, 2019 00:16
-
-
Save Jaykul/74830943ccbf40822a4d4cc1a238806c to your computer and use it in GitHub Desktop.
Using that RegularExpression MatchEvaluator in PowerShell
This file contains 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
function Measure-SubStringCount { | |
<# | |
.SYNOPSIS | |
Counts occurrences of a regex pattern in the input, shows them with -Verbose | |
.DESCRIPTION | |
Written as an example of how the Regex MatchEvalutor can be (ab)used for good. | |
#> | |
[CmdletBinding()] | |
param( | |
[string]$Pattern, | |
[Parameter(ValueFromPipeline)]$InputObject | |
) | |
begin { | |
$script:i = 0 | |
} | |
process { | |
Write-Verbose "`e[39m$([regex]::Replace($InputObject, $Pattern, { $script:i++; "`e[91m${script:i}`e[39m" }))" | |
} | |
end { | |
$script:i | |
} | |
} |
Author
Jaykul
commented
Sep 26, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment