Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Last active September 26, 2019 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jaykul/74830943ccbf40822a4d4cc1a238806c to your computer and use it in GitHub Desktop.
Save Jaykul/74830943ccbf40822a4d4cc1a238806c to your computer and use it in GitHub Desktop.
Using that RegularExpression MatchEvaluator in PowerShell
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
}
}
@Jaykul
Copy link
Author

Jaykul commented Sep 26, 2019

WindowsTerminal_2019-09-25_20-14-14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment