List Info

Thread: Python syntax question




Python syntax question
user name
2007-06-05 06:39:27
 
Does anyone know if there is a parameter I can set on the replace function that will do a case insensitive replace (old,new).
 
 
Thanks in advance.
 
 

    Robert Brook
    Electric Insurance Company
    75 Sam Fonzo Drive, Beverly, MA 01915
    phone: 978.524.5374
    fax: 978.236.5374
    robert.brookelectricinsurance.com
    http://www.electricinsurance.com

 

Re: Python syntax question
user name
2007-06-05 07:03:28
nope, but you can do it with the "re" module..

import re

compiled_regexp = re.compile("sensitive", re.IGNORECASE)

print re.sub(compiled_regexp, "foobar", "I9;m INSENSITIVE")

(should replace any flavor of "sensitive" with "foobar" -- in this case, returning "I9;m INfoobar")

On 6/5/07, Brook, Robert < Robert.Brookelectricinsurance.com">Robert.Brookelectricinsurance.com> wrote:
 
Does anyone know if there is a parameter I can set on the replace function that will do a case insensitive replace (old,new).
 
 
Thanks in advance.
 
 

    Robert Brook
    Electric Insurance Company
    75 Sam Fonzo Drive, Beverly, MA 01915
    phone: 978.524.5374
    fax: 978.236.5374
    robert.brookelectricinsurance.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">robert.brookelectricinsurance.com
    http://www.electricinsurance.com

 


_______________________________________________
boston-pig mailing list
wingware.com">boston-pigwingware.com
http://wingware.com/mailman/listinfo/boston-pig




--
I am often wrong, but I am never in doubt.
Re: Python syntax question
country flaguser name
United States
2007-06-05 07:12:52
replace() is pretty simple.&nbsp; Generally if you need more than it does, you have to jump up to re.sub(), or write your own.  You don't mention whether you want just the replacement to be case-magic:

&nbsp; &nbsp; "dog".myreplace("dog", "cat") --> "cat"
&nbsp; &nbsp; "Dog".myreplace("dog", "cat") --> "cat" or "Cat" ?
   ; "DOG".myreplace("dog", "cat") --> "cat" or "CAT" ?
   ; "DoG".myreplace("dog", "cat") --> "cat" or ?

If you want "cat" in all cases, re.sub will do the trick:

 &nbsp;  re.sub("(?i)dog", "cat", mystring)

If you want the magic replace, then you need more code:

import re

def magicreplace(str, old, new):
 &nbsp;  pattern = "(?i)" + re.escape(old)
 &nbsp; 
 &nbsp;  def replfn(matchobj):
 &nbsp; &nbsp; &nbsp;  if matchobj.group(0).islower():
 &nbsp; &nbsp; &nbsp;   ; &nbsp; return new.lower()
 &nbsp; &nbsp; &nbsp;  elif matchobj.group(0).isupper():
 &nbsp; &nbsp; &nbsp;   ; &nbsp; return new.upper()
 &nbsp; &nbsp; &nbsp;  elif matchobj.group(0).istitle():
 &nbsp; &nbsp; &nbsp;   ; &nbsp; return new.title()
 &nbsp; &nbsp; &nbsp;  else:
 &nbsp; &nbsp; &nbsp;   ; &nbsp; return new
 &nbsp; &nbsp; &nbsp; 
 &nbsp;  return re.sub(pattern, replfn, str)

assert magicreplace("my dog", "dog", "cat") == "my cat"
assert magicreplace("my Dog", "dog", "cat") == "my Cat"
assert magicreplace("my DOG", "dog", "cat") == "my CAT"
assert magicreplace("my DoG", "dog", "cat") == "my cat"
Hope this helps!

--Ned.
http://nedbatchelder.com/blog

Brook, Robert wrote:
artemis.electricinsurance.com" type="cite">
 
Does anyone know if there is a parameter I can set on the replace function that will do a case insensitive replace (old,new).
 
 
Thanks in advance.
 
 
 

_______________________________________________ boston-pig mailing list wingware.com">boston-pigwingware.com http://wingware.com/mailman/listinfo/boston-pig

-- 
Ned Batchelder, http://nedbatchelder.com
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )