-1

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

The fourth bird
  • 154,723
  • 16
  • 55
  • 70

1 Answers1

1

You can use a lookahead instead matching a and asserting b to the right without a capture group, and replace with *

a(?=b)

Regex demo

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")
The fourth bird
  • 154,723
  • 16
  • 55
  • 70