2

I have a text like this:

@110605!~~!Abhay_f!~~!Abhay%20Raj%20Fac!>>! @138547!~~!testvarun!~~!Varun%20Test%20User!>>! @136588!~~!jitendra_pathak!~~!Jitendra%20Pathak!>>! #gffj #varun okjjbbbd

and in this string I want to replace !~~! with "".

I am using a template tag and I used this method but it didn't work for me:

@register.filter("metioned_user_text_encode")
def metioned_user_text_encode(string, args):
    search = args.split(args[0])[1]
    replace = args.split(args[0])[2]

    return re.sub(search, replace, string)

In my template:-

result_data_for_editing.newsText | metioned_user_text_encode:"/l(u+)pin/m\1gen"
John Moutafis
  • 22,254
  • 11
  • 68
  • 112
Abi Waqas
  • 296
  • 2
  • 15
  • Did you consider template strings are not [raw strings](https://stackoverflow.com/a/7262918/1600649)? –  Jun 23 '17 at 10:15

3 Answers3

1

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 :)

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
0

you can use the fileinput module which makes find and replace easy

import fileinput

with fileinput.FileInput(inputFile, inplace=True, backup='.back') as f:
    for line in f:
        print(line.replace(textToSearch, textToReplace), end='')

Or you can use the replace method

str.replace("!~~!", "")
Akshay Apte
  • 1,539
  • 9
  • 24
0

try this:

a = '@110605!~~!Abhay_f!~~!Abhay%20Raj%20Fac!>>! @138547!~~!testvarun!~~!'
b = a.replace(!~~!,'""')
print(b)
Drako
  • 773
  • 10
  • 22