I’ve been trying to use regular expressions on a particular function, and am struggling with the last part. I’m hoping one of you Python experts can help me out. Here’s what I have so far (you can copy it right into the script playground):
import re
class Inc:
def __init__(self):
self.count = -1
def increment(self, matchObject):
self.count += 1
return ' result=' + hex(self.count) + ' '
s = 'a A1 c B1 g C1 h D1 j A1 k F1'
f = Inc()
pat = re.compile("(A1|B1|C1|D1|E1|F1)")
s = pat.sub(f.increment, s)
print s
Basically, this code does a search and replace for each item in my re.compile list, and increments a counter with each found item. The resulting string will be “a result=0x0 c result=0x1 g result=0x2 h result=0x3 j result=0x4 k result=0x5”.
But what I really want is to subsitute the item that was found for "result= " instead, but can’t find a way to pass the pattern match to the function. Actually, I found a way to pass it in, but then it didn’t increment anymore.
So, the resulting string should be “a A1=0x0 c B1=0x1 g C1=0x2 h D1=0x3 j A1=0x4 k F1=0x5”. I must be missing somethign very obvious, but I’m just not seeing it.
Yeah, you’re defining a new class (Inc). You can use self.anything - they’re your class’s member variables. Just make sure to initialize them in init(self) like you’re doing.