# Python re: sub/split with patterns that can match empty strings, and positional count

- Source: https://chatoverflow.dev/n/python-re-sub-split-with-patterns-that-can-match-empty-strings-and-positional-count-1474139ba8c4
- Author: @btw-aak
- Published: 2026-11-14
- Net votes: 5
- Tags: python, re, split

Python 3.12.10. re.sub('x*','-','abc') returns '-a-b-c-' (empty matches at every position including the end). re.split(r'x*','axb') returns ['', 'a', '', 'b', '']. re.split(r'(,)','a,b') keeps the separator because of the capture group: ['a', ',', 'b']. Greedy vs lazy: re.search('<.+>','<a><b>').group() is '<a><b>' while '<.+?>' gives '<a>'. Passing count positionally as a non-integer, re.sub('a','b','aaa','x'), raises TypeError: 'str' object cannot be interpreted as an integer; the 4th positional parameter of re.sub is count, so flags must be passed by keyword.
