472,807 Members | 3,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,807 software developers and data experts.

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 3059

[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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Newbie | last post by:
Dear friends, I am having a hard time understanding how to use a SELECT CASE in ASP. I have used it in VB but never in ASP scripting. Scenerio: I have 2 textboxes on a form that I have to...
19
by: Robert Scheer | last post by:
Hi. In VBScript I can use a Select Case statement like that: Select Case X Case 1 to 10 'X is between 1 and 10 Case 11,14,16 'X is 11 or 14 or 16 End Select
1
by: ST | last post by:
Hi, I'm trying to debug someone else's code, and I'm going thru this Select Case statement. I'm having problems with the "OTHER" case...in that when the first line of the case is false, it jumps...
2
by: cs168 | last post by:
Hi I am new in ASP programming so I do use the very basic and simple way to do all my stuff. Now I do really got stuck at how can I loop thru the calculation for all my selection.. My full code is as...
6
by: deanfamily11 | last post by:
I've set up a case statement to have my program determine where on the Cartesian plane a point the user enters is located. I keep getting the C2051 error when I compile. Any help? #include...
15
by: YitzHandel | last post by:
The following is an extract from my program, which I seperated out and compiled as its own program. The problem is that as soon as a case tests true, all the cases below it execute as well. So...
10
by: MLH | last post by:
Suppose the following... Dim A as Date A=#7/24/2005# I wish to compare value of A against 2 other values: 1) 8/1/2005 2) 9/1/2005 Which is better and why... First:
7
by: Lauren Quantrell | last post by:
Is there any speed/resource advantage/disadvantage in using Select Case x Case 1 Case 2 etc. many more cases... End Select VS.
22
by: John | last post by:
Hi Folks, I'm experimenting a little with creating a custom CEdit control so that I can decide on what the user is allowed to type into the control. I started off only allowing floating point...
24
by: clockworx05 | last post by:
Hey guys i have this program that i need to write for class. here are the instructions: Write a function called foo that asks the user for their age. Pass the age value to a function called...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.