Re.compile

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.

Geez, I think I got it now. It figures, right after I posted it. If I replace the return value with

“return matchObject.group() + '= ’ + hex(self.count) + ’ '”, it does exactly what I need it to.

But now, how can I insert that return value into a list? For instance, by changing the above code a bit, I can print the result of each iteration:

import re
class Inc: 
	def __init__(self): 
		self.count = -1 

	def increment(self, matchObject): 
		self.count += 1
		print matchObject.group() + '= ' + hex(self.count) + ' '
		return matchObject.group() + '= ' + 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) 

The results would look like this:

A1= 0x0
B1= 0x1
C1= 0x2
D1= 0x3
A1= 0x4
F1= 0x5
A1= 0x0

To put them in a list, would I declare a list variable as part of the class? Or as part of increment?

You could do it various ways. I would do it like this:

[code]import re
class Inc:
def init(self):
self.count = -1
self.list = []

def increment(self, matchObject):
self.count += 1
self.list.append(matchObject.group() + '= ’ + hex(self.count))
return matchObject.group() + '= ’ + 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)

myList = f.list[/code]

Hope this helps,

Awesome. I had no idea that I could use self.list that way. It opens up all kinds of possibilities. Thanks!

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.