See your regex matches in color

A quick function to show you your regular expression matches in color:

function show-match {
param (
[parameter(Mandatory=$true,
ValueFromPipeline=$true)]
[AllowEmptyString()]
[string]$string,
[parameter(Mandatory=$true,
Position=1)]
[regex]$regex
)
begin{
$input_bg = 15
$input_fg = 0
$gbc = 07,01,02,03,04,05,08,09,10
$gfc = 15,14,14,14,14,14,14,14,14
}

process{
$bg_colors = [collections.arraylist]::repeat($input_bg,$string.length)
$fg_colors = [collections.arraylist]::repeat($input_fg,$string.length)

$m = [regex]::matches($string,$regex)

foreach ($match in $m){
$i=0
foreach ($group in $match.groups){
for ($n=$group.index;$n -lt $group.index + $group.length;$n++){
$bg_colors[$n] = $gbc[$i]
$fg_colors[$n] = $gfc[$i]
}
if ($i -le $gbc.count){$i++}
}
}

for ($char=0;$char -lt $string.length;$char++){
write-host $string[$char] -foregroundcolor $fg_colors[$char] -backgroundcolor $bg_colors[$char] -nonewline
}
write-host “”
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s