-4

I want to remove triangular quotes and their contents. Here is an example string:

Example ^x^ string

I need: Example string

xss1de
  • 13
  • 3

1 Answers1

0

You can use the re.sub to search for a regex patter and substitute it:

import re

s = "test^1^123"
result = re.sub("\^.*\^", "", s)
print(result)  # test123

The first argument to re.sub is the pattern (in this case a literal ^ followed by any number of any other characters and another literal ^) and the second is the string to replace the pattern with (in this case an empty string)

If multiple triangle quoted strings are possible in the same imput then the regex should be non-greedy:

import re

s = "test^1^123"
result = re.sub("\^.*?\^", "", s)
print(result)  # test123
Matteo Zanoni
  • 3,429
  • 9
  • 27