473,698 Members | 2,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\Pr ofiles\default. uyw\extensions\ {0538E3E3-7E9B-4d49-8831-A227C80A7AD3}\c omponents\nsFor ecastfox.js,111 1185900000
abs:J:\Firefox\ Firefox_Data\Pr ofiles\default. uyw\extensions\ {c4dc572a-3295-40eb-b30f-b54aa4cdc4b7}\c omponents\wml-service.js,1114 705020000

to:

rel:nsForecastf ox.js,111118590 0000
rel:wml-service.js,1114 705020000

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(theR egEx, 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 3361
<tc****@gmail.c om> wrote:

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

lines such as:

abs:J:\Firefox\ Firefox_Data\Pr ofiles\default. uyw\extensions\ {0538E3E3-7E9B-4d49-8831-A227C80A7AD3}\c omponents\nsFor ecastfox.js,111 1185900000
abs:J:\Firefox\ Firefox_Data\Pr ofiles\default. uyw\extensions\ {c4dc572a-3295-40eb-b30f-b54aa4cdc4b7}\c omponents\wml-service.js,1114 705020000

to:

rel:nsForecastf ox.js,111118590 0000
rel:wml-service.js,1114 705020000


Try this:

import re
from fileinput import FileInput

regex = re.compile(r'^a bs:.*\\(.+)$')
input = FileInput(filen ame)
unparsed = []

for line in input:
try:
print regex.match(lin e).group(1)
except:
unparsed.append (input.fileline no())
print line

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

George

Jul 21 '05 #2
Am Tue, 12 Jul 2005 01:11:44 -0700 schrieb tc****@gmail.co m:
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\Pr ofiles\default. uyw\extensions\ {0538E3E3-7E9B-4d49-8831-A227C80A7AD3}\c omponents\nsFor ecastfox.js,111 1185900000
abs:J:\Firefox\ Firefox_Data\Pr ofiles\default. uyw\extensions\ {c4dc572a-3295-40eb-b30f-b54aa4cdc4b7}\c omponents\wml-service.js,1114 705020000

to:

rel:nsForecastf ox.js,111118590 0000
rel:wml-service.js,1114 705020000


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(theR egEx, 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(fileToSear ch, "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(fileToSear ch, "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
8723
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, returned the hyperlinked page title, filename, and the body txt (30 preceding and following words) in context with the search word highlighted. Excellent.! See it working at: http://www.ipt.co.za Just search for "firearm"
13
4529
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 = RegEx.Replace(vSearch, "<span class=""Highlight"">" & vFind &
4
7110
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 files under \Program Files and it's subdirectories that meet the following criteria:
32
14852
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
3
8255
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;}") Thanks, Craig
4
4833
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 way mean to secrife lots of time in asp.net. can any one give me such a expression in which i pass a data string and search word string and replace word string? if so plz help me out. i'm in badly need.
2
5109
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 this. I have tried using the Like comparision operator but about all I can do with it is replace a whole string that contains the wildcard search string. I want to do something like finding *mysea?rch in a string like "This is mysearch string...
6
5886
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". This parrot is sleeping. Really, it is sleeping. to This parrot is dead. Really, it is dead.
7
4612
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 BeautifulSoup to clean those up? There are various parsing options related to "&" handling, but none of them seem to do quite the right thing. If I write the BeautifulSoup parse tree back out with "prettify", the loose "&" is still in there. So...
0
8674
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7721
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.