Unless I completely misunderstood your intentions, you are asking one thing but your code shows a different expected behavior than the one you ask for.
I will try to answer both cases:
Case 1:
You expect to get a multitude of separators like !~~!
or !>>!
etc., and you need a regex to replace all of them with an empty string (''
).
Try the following:
import re
SEPARATORS = [
'!~~!',
'!>>!',
other separators...
]
@register.filter("metioned_user_text_encode")
def metioned_user_text_encode(string):
return re.sub('|'.join(SEPARATORS), '', string)
Explanation:
- The
|
regex operator ensures that our pattern will try to match every separator given in SEPARATORS
with the given string (s
).
- The
re.sub()
method will return the string with every pattern matching any of our SEPARATORS
, replaced by the empty string.
Case 2:
You will receive as arguments a series of separators and an equally sized series of replacements for those separators. In that case, try:
@register.filter("metioned_user_text_encode")
def metioned_user_text_encode(string, args):
returned_string = string
search = args.split(args[0])[1]
replace = args.split(args[0])[2]
for i in range(len(search)):
returned_string = re.sub(search[i], replace[i], returned_string)
return returned_string
Explanation:
- The
for
loop will traverse the search
and replace
lists for every separator and the corresponding replacement.
- The
re.sub()
method will return in each iteration, the returned_string
with the search[i]
separator replaced by the replace[i]
substitute.
Good luck :)