example
re.sub("(a)(b)", "*", "abc")
group 1 (a)
group 2 (b)
i want to change group1 to "*", only
answer is *bc
What should I do?
plz, give me a hint
example
re.sub("(a)(b)", "*", "abc")
group 1 (a)
group 2 (b)
i want to change group1 to "*", only
answer is *bc
What should I do?
plz, give me a hint
You can use a lookahead instead matching a and asserting b to the right without a capture group, and replace with *
a(?=b)
import re
re.sub("a(?=b)", "*", "abc")
Output
*bc
Or using a single capture group for what you want to keep, and use that in the replacement using \1
Note that you don't have to capture what you don't want in the replacement.
re.sub("a(b)", r"*\1", "abc")