简体   繁体   中英

Python - How to match and replace words from a given string?

I have a array list with large collection, and i have one input string. Large collecion if found in the input string, it will replace by given option.

I tried following but its returning wrong:

#!/bin/python
arr=['www.', 'http://', '.com', 'many many many....']
def str_replace(arr, replaceby, original):
  temp = ''
  for n,i in enumerate(arr):
    temp = original.replace(i, replaceby)
  return temp

main ='www.google.com'
main1='www.a.b.c.company.google.co.uk.com'
print str_replace(arr,'',main);

Output:

www.google

Expected:

google

You are deriving temp from the original every time, so only the last element of arr will be replaced in the temp that is returned. Try this instead:

def str_replace(arr, replaceby, original):
  temp = original
  for n,i in enumerate(arr):
    temp = temp.replace(i, replaceby)
  return temp

You don't even need temp (assuming the above code is the whole function):

def str_replace(search, replace, subject):
    for s in search:
        subject = subject.replace(s, replace)
    return subject

Another (probably more efficient) option is to use regular expressions:

import re

def str_replace(search, replace, subject):
    search = '|'.join(map(re.escape, search))
    return re.sub(search, replace, subject)

Do note that these functions may produce different results if replace contains substrings from search .

temp = original.replace(i, replaceby)

It should be

temp = temp.replace(i, replaceby)

You're throwing away the previous substitutions.

Simple way :)

arr=['www.', 'http://', '.com', 'many many many....']
main ='http://www.google.com'
for item in arr:
    main = main.replace(item,'')
print main

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM