Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Advanced Search and replace technique(help!)

Name: Anonymous 2010-04-20 5:38

                        <option value="Seller">Seller</option>
                        <option value="Seller2">Seller2</option>
                        <option value="xxx">Seller3</option>
                        <option value="xxx">sedfBFB</option>
                        <option value="xxx">Sedscdsa</option>
                        <option value="xxx">dfsdvs</option>
                        <option value="xxx">sg42gr</option>
On those line(plus many more ofcourse) I would like to replace xxx with the text that comes next, like the first two lines.
With simple replace of text editors this can be done.
Can this be done with emacs something like that?
Something like replace xxx with what is wrapped by > and </option>.Or just copy what is wrapped between two string would be enough.

Name: new to ``re`` module 2010-04-20 6:23

[code=python]
from re import match

def foo(s):
    def bar(gs):
        if len(gs) == 2 and gs[0] == "xxx":
            return s.replace("xxx", gs[1])
        return s
    return bar(match("\s*\<option value\=\"(\w+)\"\>(\w+)\<\/option\>\s*$", s).groups())

with open("src") as fs, open("dst", mode="w") as fd:
    fd.writelines(map(foo, fs.readlines()))
[/code]
It works, but doesn't fully satisfy me. Is there a neat way to get rid of that dumb replace?

Name: Anonymous 2010-04-20 6:24

Oh, Jesus! Terribly sorry!

from re import match

def foo(s):
    def bar(gs):
        if len(gs) == 2 and gs[0] == "xxx":
            return s.replace("xxx", gs[1])
        return s
    return bar(match("\s*\<option value\=\"(\w+)\"\>(\w+)\<\/option\>\s*$", s).groups())

with open("src") as fs, open("dst", mode="w") as fd:
    fd.writelines(map(foo, fs.readlines()))

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List