Case-Sensitive Sarch and replace

T

Thomas Philips

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(),replacestring), 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
 
R

Richie Hindle

[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:
.... regex = '(?i)' + re.escape(search)
.... return re.sub(regex, replace, filename)
.... 'AlphaMax.txt'
 
H

Harry George

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(),replacestring), 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')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top