How can I index group elements in re.sub?

how can I index a character in a matched group from within the re.sub match.

So I am trying to do this:

import re
string_test = "this.day"
string_test = re.sub("^(.*?).(.*?)", "g<1>[0].g<2>", string_test)

My result should be: "t.day". Is it possible to somehow index the g<1> group within the re.sub ? Obviously g<1>[0] doesn’t work.


Source: regex

Leave a Reply