re.sub(pattern, repl, string, count=0, flags=0)
The optional argument count is the maximum number of pattern occurrences to be replaced; count must be a non-negative integer. If omitted or zero, all occurrences will be replaced. Empty matches for the pattern are replaced only when not adjacent to a previous empty match, so sub('x*', '-', 'abxd') returns '-a-b--d-'.
Changed in version 3.7: Empty matches for the pattern are replaced when adjacent to a previous non-empty match.
python
1>>> import sys
2>>> sys.version
3'3.10.6 (main, Aug 10 2022, 11:40:04) [GCC 11.3.0]'
4
5>>> import re
6>>> name = 'hoge.jpg'
7>>> print(re.sub(r'[^.]*$', 'webp', name, count=1))
8hoge.webp