-2

I am trying to match and replace all content tagged inside brackets with the following code:

content = 'Should replace [obj[text]obj] inside [loc[brackets]loc]'
pattern = re.compile(r"\[([A-Za-z0-9]+)(\[(.*?)\])([A-Za-z0-9]+)\]")
for match in pattern.finditer(content):
  print(match.group(2))
  re.sub(match.group(2), 'xxx', content)

However it keeps returning the very same original string. If I go like:

new=re.sub(match.group(2), 'xxx', content)

It returns something really random I can't still acount for. It gets me puzzled because the print in line 4 shows the findigs are correct.

Many thanks in advance.

chancar
  • 1
  • 1
  • The first argument to `re.sub()` should be the regular expression, not something that was found with `finditer()`. – Barmar May 31 '22 at 22:30
  • What is your intended result? – Barmar May 31 '22 at 22:30
  • Are you surprised that `re.sub("[text]", "xxx", content)` replaces all `e` characters with `xxx`? `[text]` is a character range expression just like you used in `[A-Za-z0-9]` – that other guy May 31 '22 at 22:32

1 Answers1

0

You shouldn't be looping. Just call re.sub() once, and it will replace all occurrences of the regular expression. Use back-references in the replacement string to copy parts of the match into the replacement.

new = re.sub(pattern, r'[\1xxx\4]', content);

DEMO

Barmar
  • 669,327
  • 51
  • 454
  • 560