简体   繁体   中英

Replacing words containing at least one element of a list - Python / RegEx

I have a list of special characters. I want to apply a RegEx function that replaces words containing at least one element of a list with a comma (",").

So far, using the following code, I know how can I replace characters but I do not know how to do this with the entire word.


characters_list = ['%' , ':' , '(']

text = "Fun%( fact: About 71,3% of the Earth's surface is water-covered: and the oceans hold about 96.5% of all Earth's water."


regex = re.compile('|'.join(map(re.escape,characters_list)))
text = regex.sub(",", text)


I want the string "text" to become:

", , About , of the Earth's surface is , and the oceans hold about , of all Earth's water."

(All the words containing at least one element from my list "characters_list" have been changed to a comma)

Here is a solution without regex

>>> ' '.join((',' if any(c in word for c in characters_list) else word) for word in text.split())
", , About , of the Earth's surface is , and the oceans hold about , of all Earth's water."

Using re.findall :

' '.join([',' if re.findall('|'.join(map(re.escape,characters_list)), s) else s 
          for s in text.split(' ')])

Output:

", , About , of the Earth's surface is , and the oceans hold about , of all Earth's water."

Using re.sub :

import re

characters_list = ['%' , ':' , '(']
text = "Fun%( fact: About 71,3% of the Earth's surface is water-covered: and the oceans hold about 96.5% of all Earth's water."

print(re.sub( '|'.join(r'(?:[^\s]*{}[^\s]*)'.format(re.escape(c)) for c in characters_list), ',', text ))

Prints:

, , About , of the Earth's surface is , and the oceans hold about , of all Earth's water.

I think, If you make your code more human-readable, write your code just like this:

import re

text = "Fun%( fact: About 71,3% of the Earth's surface is water-covered: and the oceans hold about 96.5% of all Earth's water."
replaced = re.sub(r'\S+(%|:|\()', ',', text)

print(replaced) 

Output :

, , About , of the Earth's surface is , and the oceans hold about , of all Earth's water.

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