469,920 Members | 2,239 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,920 developers. It's quick & easy.

Case-Sensitive Sarch and replace

Using glob(), I obtain a list of filenames with some characters in
upper case, others in lower case. I want to rename the files by
replacing one substring in each filename with another, but with two
twists.

1. The search must be case insensitive
2. After portion of the filename that does not match the search string
must not have its case changed.

For example, if fn="AlphaMin.txt", searchstring="min" and
replacestring= "Max", I want the file to be renamed "AlphaMax.txt" and
not "alphaMax.txt" or "alphamax.txt"

I can easily get alphaMax.txt by using
fn.lower().replace(searchstring.lower(),replacestr ing), but then the
portion of fn that is not being replaced is lowercased.

It's not hard to write a function that repeatedly finds
searchstring.lower() in fn.lower(), and then uses slices to replace
the appropriate portions of fn, but there must be a simpler and
cleaner way to acheive this goal. Suggestions?

Thomas Philips
Jul 18 '05 #1
2 2910

[Thomas]
For example, if fn="AlphaMin.txt", searchstring="min" and
replacestring= "Max", I want the file to be renamed "AlphaMax.txt" and
not "alphaMax.txt" or "alphamax.txt"


Use case-insensitive regular expression replacement:
import re
def replace(filename, search, replace): .... regex = '(?i)' + re.escape(search)
.... return re.sub(regex, replace, filename)
.... replace("AlphaMin.txt", "min", "Max")

'AlphaMax.txt'

--
Richie Hindle
ri****@entrian.com
Jul 18 '05 #2
tk****@hotmail.com (Thomas Philips) writes:
Using glob(), I obtain a list of filenames with some characters in
upper case, others in lower case. I want to rename the files by
replacing one substring in each filename with another, but with two
twists.

1. The search must be case insensitive
2. After portion of the filename that does not match the search string
must not have its case changed.

For example, if fn="AlphaMin.txt", searchstring="min" and
replacestring= "Max", I want the file to be renamed "AlphaMax.txt" and
not "alphaMax.txt" or "alphamax.txt"

I can easily get alphaMax.txt by using
fn.lower().replace(searchstring.lower(),replacestr ing), but then the
portion of fn that is not being replaced is lowercased.

It's not hard to write a function that repeatedly finds
searchstring.lower() in fn.lower(), and then uses slices to replace
the appropriate portions of fn, but there must be a simpler and
cleaner way to acheive this goal. Suggestions?

Thomas Philips


Try using regular expressions:

data=["AlphaMin.txt","alphaMin.txt","Alphamin.txt"]
min_pat=re.compile(r'(min)',re.I)
for name in data:
msg("name=%s " % name)
m=min_pat.search(name)
cnt=len(m.groups())
if cnt==0:
msg('no match')
elif cnt>1:
msg('found more than one, need instructions')
else:
newname=min_pat.sub('Max',name)
msg('newname=%s ' % newname)
msg('\n')
--
ha************@boeing.com
6-6M21 BCA CompArch Design Engineering
Phone: (425) 342-0007
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Newbie | last post: by
19 posts views Thread by Robert Scheer | last post: by
1 post views Thread by ST | last post: by
6 posts views Thread by deanfamily11 | last post: by
15 posts views Thread by YitzHandel | last post: by
7 posts views Thread by Lauren Quantrell | last post: by
22 posts views Thread by John | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.