From 6a6d8f8d620b4aec9de10c8af3407d4d16ec387c Mon Sep 17 00:00:00 2001 From: Charles Lewis Date: Mon, 10 Jun 2013 21:45:04 -0400 Subject: [PATCH] bugfix #40: don't try to color non-matching groups --- colout/colout.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/colout/colout.py b/colout/colout.py index 9bf991c..e06dfbf 100755 --- a/colout/colout.py +++ b/colout/colout.py @@ -484,8 +484,10 @@ def colorup(text, pattern, color="red", style="normal", on_groups=False): # but that match.start(0) refers to the whole match, the groups being indexed in [1,n]. # Thus, we need to range in [1,n+1[. for group in range(1, nb_groups+1): - partial, end = colorout(text, match, end, group_colors[group-1], group_styles[group-1], group) - colored_text += partial + # If a group didn't match, there's nothing to color + if match.group(group) is not None: + partial, end = colorout(text, match, end, group_colors[group-1], group_styles[group-1], group) + colored_text += partial # Append the remaining part of the text, if any. colored_text += text[end:]