0

I have a string like like:

Hello +++this--- is a funny +++string--- with words inside

I need to look for words surrounded by +++ and --- and replace the +++ and --- signs with html tags like <a>word</a>.

So that the text above becomes:

Hello <a>this</a> is a funny <a>string</a> with words inside

I'm currently using the following regex with a sub instruction:

rgx = re.compile(r'(?:\+\+\+)(?P<word>.+)(?:\-\-\-)')
output = rgx.sub('<a>\\g<word></a>')

And the output I get is:

Hello <a>this--- is a funny +++string</a> with words inside

As you can see, only the first and last +++/--- are taken into account. The ones in the middle seems to be ignored. Obviously the big string between first and last +++/--- match my regex.

How can I get re to match all groups one by one, and not consider the big overlapping match?

Note: I've tried with ^\+ and ^\- in the capturing group, but it doesn't work because a + or - could be in any word and should be kept as is, as long as it's not +++ or ---.

Jivan
  • 21,522
  • 15
  • 80
  • 131

0 Answers0