Search & Replace with RegEx

T

tchurm

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
 
G

George Sakkis

[snipped]
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

Try this:

import re
from fileinput import FileInput

regex = re.compile(r'^abs:.*\\(.+)$')
input = FileInput(filename)
unparsed = []

for line in input:
try:
print regex.match(line).group(1)
except:
unparsed.append(input.filelineno())
print line

print "Unparsed lines:", ','.join(map(str,unparsed))

George
 
T

Thomas Guettler

Am Tue, 12 Jul 2005 01:11:44 -0700 schrieb (e-mail address removed):
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

Hi,

some time ago I wrote "replace-recursive.py":

http://www.thomas-guettler.de/scripts/replace_recursive.py.txt

Maybe this helps you,

Thomas
 
T

tchurm

thanks for the comments + help.

i think i got it working, although it's not pretty:

##############################
import os
import re

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

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

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

#set this to an empty string
newdoc = ""

if os.path.isfile( fileToSearch ):
x = open(fileToSearch, "r")
while 1:
line = x.readline()
if(p.match(line)!=None):
print ""
print line
newdoc = newdoc + re.sub("abs:.*\.*.\\\\", "rel:", line)
else: newdoc = newdoc + line
if line == "":
print ""
print "finished !"
break

x.close()

if newdoc != '':
x = open(fileToSearch, "w")
x.write(newdoc)
x.close()
print ""
print "file written!"


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

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,054
Latest member
TrimKetoBoost

Latest Threads

Top