From cf3370988b7dcac6c8c1e730fce4f2ff144a4a7c 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 0f7cfc3..0ac5a39 100755 --- a/colout/colout.py +++ b/colout/colout.py @@ -304,8 +304,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:]