-3

I'm trying to strip the following;

INPUT:

[color=00ff08]100[/color](3.0/3.0)

The =00ff08 will change given the color, so that has to be variable.

OUTPUT:

100(3.0/3.0)

Basically I want to remove [color=*****] and [/color] from the string.

Thanks, I'm so horrible at regex. Maybe I should get a book.

Ali Shahrivarian
  • 311
  • 2
  • 11
Carl Von
  • 170
  • 9

3 Answers3

1

Using re.sub

Demo:

import re

s = """INPUT [color=00ff08]100[/color](3.0/3.0)"""

text = re.sub("\[color=.*?\]", "", s)
text = re.sub("\[/color\]", "", text)  

print(text)
Rakesh
  • 78,594
  • 17
  • 67
  • 103
0
re.sub(r'(.*)'
       r'\[color=[^\]]*\]'
       r'([^\[]*)'
       r'\[/color\]'
       r'(.*)', r'\1\2\3', my_string)

This captures the relevant parts into groups, ignores the remaining parts and stitches the result together again.

Doing it like this doesn't cloak problems in the input. If you like a more robust solution (which also works decently for slightly wrong input), have a look at @Rakesh's answer. His would also work for inputs like INPUT 100[/color][/color](3.0/3.0) etc.

Alfe
  • 52,016
  • 18
  • 95
  • 150
0

Here's something that works for all tags:

a = "[color=00ff08]100[/color](3.0/3.0)"
re.sub("\\[[^]]*]", "", a)

Output:

'100(3.0/3.0)'
Ashish Acharya
  • 3,219
  • 1
  • 14
  • 24