How do I match this with re

S

Sunit Joshi

Hello All
I have a problem here where I need to find the drive letters in a text
file and replace them with another letter. Now the lines can be as
long as this

6 'C:\pds\bddin64\report\mto\' 'SJOSHI'
'D:\pds\bddin64\report\format\' 'SJOSHI'

with multiple entries for path; i.e. there may be 2, as above or 1 or
more then 2. If I use the expression .*'([A-Z]:).*?', it only matches
the 2nd D: How do I get all the path groups ..??


thanks
Sunit
 
T

Terry Reedy

Sunit Joshi said:
Hello All
I have a problem here where I need to find the drive letters in a text
file and replace them with another letter. Now the lines can be as
long as this

6 'C:\pds\bddin64\report\mto\' 'SJOSHI'
'D:\pds\bddin64\report\format\' 'SJOSHI'

with multiple entries for path; i.e. there may be 2, as above or 1 or
more then 2. If I use the expression .*'([A-Z]:).*?', it only matches
the 2nd D: How do I get all the path groups ..??

Use file.readline() or .readlines() and then process one line at a
time.

Terry
 
C

Christos TZOTZIOY Georgiou

Hello All
I have a problem here where I need to find the drive letters in a text
file and replace them with another letter. Now the lines can be as
long as this

6 'C:\pds\bddin64\report\mto\' 'SJOSHI'
'D:\pds\bddin64\report\format\' 'SJOSHI'

with multiple entries for path; i.e. there may be 2, as above or 1 or
more then 2. If I use the expression .*'([A-Z]:).*?', it only matches
the 2nd D: How do I get all the path groups ..??

I assume that the drive letters will be directly after a single quote.
You need this re:

reDriveLetter = re.compile(r"(?<=')[A-Z](?=:\\)").

You can try reDriveLetter.findall() or the reDriveLetter.finditer()
functions then, each match object will match a single capital letter.

Very important to everyone working with regular expressions: the
redemo.py script. Search for a Scripts directory in your python
directories (it's under Tools on Windows, otherwise get it from the
source distribution), redemo.py is in there along with other useful
scripts.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top