regex into str

Peter Kleiweg in.aqua.scribis at nl.invalid
Sat Aug 28 20:38:27 EDT 2004


I want to use regular expressions with less typing. Like this:

    A / 'b.(..)'     # test for regex 'b...' in A
    A[0]             # get the last whole match
    A[1]             # get the first group in the last match

    A /= 'b.','X',1  # replace first occurence of regex 'b.'
                     #    in A with 'X'
    A /= 'b.','X'    # replace all occurences of regex 'b.'
                     #    in A with 'X'

This works fine if I create a class derived from 'str' and put
in the right functions. I have a demonstration below.

But what I really want is to insert these functions into class
'str' itself, so I can use them on ordinary strings:

    def __div__(self, regex):
        p = re.compile(regex)
        self.__sre__ = p.search(self)
        return str(self.__sre__.group())

    setattr(str, '__div__', __div__)

But when I try this I get:

    TypeError: can't set attributes of built-in/extension type 'str'

I there a way to get this done?




Working example:


#!/usr/bin/env python

import re

class Mystr(str):
    def __div__(self, regex):
        p = re.compile(regex)
        self.sre = p.search(self)
        return Mystr(self.sre.group())

    def __idiv__(self, tpl):
        try:
            regex, repl, count = tpl
        except ValueError:
            regex, repl = tpl
            count = 0
        p = re.compile(regex)
        return Mystr(p.sub(repl, self, count))

    def __call__(self, g):
        return self.sre.group(g)

if __name__ == '__main__':
    a = Mystr('abcdebfghbij')
    print "a :", a

    print "Match a / 'b(..)(..)' :",
    print a / 'b(..)(..)'              # find match

    print "a[0], a[1], a[2] :",
    print a[0], a[1], a[2]             # print letters from string

    print "a(0), a(1), a(2) :",
    print a(0), a(1), a(2)             # print matches

    print "a :", a

    a /= 'b.', 'X', 1     # find and replace once
    print "a :", a

    a /= 'b.', 'X'        # find and replace all
    print "a :", a



-- 
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/~kleiweg/ls.html




More information about the Python-list mailing list