Search & Replace with RegEx

George Sakkis gsakkis at rutgers.edu
Tue Jul 12 14:33:59 CEST 2005


<tchurm at 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\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




More information about the Python-list mailing list