0

I have the following string

a = "animal: [cat.], animal:[dog.]"
print(a)

>>> animal: [cat.], animal:[dog.]

I would like to replace each part of the string between [ and .] (included) with a given value (e.g. frog).

The expected output is:

animal: frog, animal: frog

I tried the following so far:

import re

b = re.sub(r'(\[\b).*(\b\.])','frog', a)
print(b)

>>> animal: frog

That it slightly different from the expected output.

I think this is due to the fact the code see the first [ and the last .] as delimiters, consequently replacing with frog all the string between.

Instead, I would like the code to consider two pairs of delimiters: those containing the word cat and those containing the word dog.

Do you have any suggestions?

Bernheart
  • 607
  • 1
  • 8
  • 17
  • 1
    `.*` in regular-expression is greedy. This means that it will search for the "longest match". Try replacing the regex with this: `r'(\[\b).*?(\b\.])'`. `?` Means that `*` will be non-greedy. – Hampus Larsson May 15 '19 at 15:26

1 Answers1

2

You should change .* to .*? as .* matches everything greedily and will consume parts you don't want to match. Also better to remove groups from your regex as you aren't using them as it makes the regex perform better.

So, you can use \[\b.*?\b\.] and replace it with frog or anything as you like.

Regex Demo

Check your updated Python code,

import re

s = 'animal: [cat.], animal:[dog.]'

b = re.sub(r'\[\b.*?\b\.]','frog', s)
print(b)

Prints,

animal: frog, animal:frog
Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36