473,385 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Search & Replace with RegEx

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\e xtensions\{0538E3E3-7E9B-4d49-8831-A227C80A7AD3}\components\nsForecastfox.js,11111859 00000
abs:J:\Firefox\Firefox_Data\Profiles\default.uyw\e xtensions\{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

Jul 21 '05 #1
3 3340
<tc****@gmail.com> wrote:

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

lines such as:

abs:J:\Firefox\Firefox_Data\Profiles\default.uyw\e xtensions\{0538E3E3-7E9B-4d49-8831-A227C80A7AD3}\components\nsForecastfox.js,11111859 00000
abs:J:\Firefox\Firefox_Data\Profiles\default.uyw\e xtensions\{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

Jul 21 '05 #2
Am Tue, 12 Jul 2005 01:11:44 -0700 schrieb tc****@gmail.com:
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\e xtensions\{0538E3E3-7E9B-4d49-8831-A227C80A7AD3}\components\nsForecastfox.js,11111859 00000
abs:J:\Firefox\Firefox_Data\Profiles\default.uyw\e xtensions\{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/script...cursive.py.txt

Maybe this helps you,

Thomas

--
Thomas Güttler, http://www.thomas-guettler.de/
Jul 21 '05 #3
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...' )
##############################

Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
13
by: David Morgan | last post by:
Hello I have a little function to highlight text if it exists. Function Highlight(vFind, vSearch) Dim RegEx Set RegEx = New RegExp RegEx.Pattern = vFind RegEx.IgnoreCase = True Highlight =...
4
by: Ben Fidge | last post by:
Hi What is the most efficient way to gather a list of files under a given folder recursively with the ability to specify exclusion file masks? For example, say I wanted to get a list of all...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
3
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&amp;") Regex.Replace(Subject, "\'", "&apos;") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&amp;|&apos;}")...
4
by: lucky | last post by:
hi there!! i'm looking for a code snipett wich help me to search some words into a particular string and replace with a perticular word. i got a huge data string in which searching traditional...
2
by: Dennis | last post by:
I am trying to implement a "Find and Replace" dialog that allows using wildcards in the find string, much like the Find and Replace Dialogs in Ms Word, etc. Are there any references or examples on...
6
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead"....
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.