Multiple re.sub () statements

In my program, the user enters a term that I process before sending. Part of this process is to change all instances of "and", "or" and "no" to uppercase letters, but leaving them intact.

I can't use string.upper() because it changes everything to uppercase; or string.replace() , because if "and" is in another word in a string, for example. the salamander will also change this to salamANDer. I believe my best option is the re.sub() function. This allows me to change complete words that are perfect. The next problem: I need to execute the re.sub() function for every change I want to make. Can I make one expression to make all the changes? What I did is not mistaken, but I do not think its necessarily good practice:

 >>import urllib2 >>import re >>query = 'Lizards and Amphibians not salamander or newt' >>query=re.sub(r'\bnot\b', 'NOT',query) >>query=re.sub(r'\bor\b', 'OR',query) >>query=re.sub(r'\band\b', 'AND',query) >>query = urllib2.quote("'"+query+"'") >>print query %27Lizards%20AND%20Amphibians%20NOT%20salamander%20OR%20newt%27 
+4
source share
1 answer

You can pass the substitution expression to the function in re.sub() :

 >>> term = "Lizards and Amphibians not salamander or newt" >>> re.sub(r"\b(not|or|and)\b", lambda m: m.group().upper(), term) 'Lizards AND Amphibians NOT salamander OR newt' 

However, I would probably go with a non-repressive solution:

 >>> " ".join(s.upper() if s.lower() in ["and", "or", "not"] else s ... for s in term.split()) 'Lizards AND Amphibians NOT salamander OR newt' 

It also normalizes spaces and works with mixed case words such as And .

+17
source

All Articles