Search & Replace with RegEx

tchurm at gmail.com tchurm at gmail.com
Tue Jul 12 10:11:44 CEST 2005


Hi Pythonistas,

Here's my problem: I'm using a version of MOOX Firefox
(http://moox.ws/tech/mozilla/) that's been modified to run completely
from a USB Stick. It works fine, except when I install or uninstall an
extension, in which case I then have to physically edit the compreg.dat
file in my profile directory, replacing all instances of Absolute Path
links to relative ones. (And, yes, I have filed a Bugzilla report.)

For example, after installing a new extension, I change in compreg.dat

lines such as:

abs:J:\Firefox\Firefox_Data\Profiles\default.uyw\extensions\{0538E3E3-7E9B-4d49-8831-A227C80A7AD3}\components\nsForecastfox.js,1111185900000
abs:J:\Firefox\Firefox_Data\Profiles\default.uyw\extensions\{c4dc572a-3295-40eb-b30f-b54aa4cdc4b7}\components\wml-service.js,1114705020000

to:

rel:nsForecastfox.js,1111185900000
rel:wml-service.js,1114705020000

I started a Python script that searches compreg.dat and finds all the
lines that I need to edit. I just need some help using RegEx to search
and replace these lines (as in the examples above), so that I no longer
need to edit compreg.dat manually.

Here's the script that I started:

################################################

import os
import re
import fileinput

theRegEx = '.*abs:.*\.*.js.*'

p = re.compile(theRegEx, re.IGNORECASE)

fileToSearch  = 'compreg.dat'
print "File to perfrom search-and-replace on: " + fileToSearch
print "Using Regex: " + theRegEx
print ""

if os.path.isfile( fileToSearch ):
	tempFile = open( fileToSearch, 'r' )

for line in fileinput.input( fileToSearch ):
    if (p.match(line)!=None):
        print line

tempFile.close()

raw_input( '\n\nPress Enter to exit...' )

################################################

I'd be very glad to get a helping hand here ;)

Thanks,

Tom




More information about the Python-list mailing list