Hmm, I'm not entirely sure what that "non-overlapping occurence of pattern" actually means but:

```python
>>> re.sub(BOLDCODE_RE, r'<b>\2</b>', '****foobar****')
'<b></b>foobar<b></b>'
>>> re.sub(r'(\*\*)(.*?)(\*\*)', r'<b>\2</b>', '**foo** and **bar**')
'<b>foo</b> and <b>bar</b>'
>>> def replacement(match): return '<b>%s</b>' % match.group(2)
...
>>> re.sub(BOLDCODE_RE, replacement, '****foobar****')
'<b></b>foobar<b></b>'
>>> re.sub(r'(\*\*)(.*?)(\*\*)', replacement, '**foo** and **bar**')
'<b>foo</b> and <b>bar</b>'
```