tags:

views:

160

answers:

2

I wanto to match the last occurence of a simple pattern in a string, e.g.

list = re.findall(r"\w+ AAAA \w+", "foo bar AAAA foo2 AAAA bar2)
print "last match: ", list[len(list)-1]

however, if the string is very long, a huge list of matches is generated. Is there a more direct way to match the second occurence of "AAAA" or should I use this workaround?

+4  A: 

you could use $ that denotes end of the line character:

>>> s = """foo bar AAAA
foo2 AAAA bar2"""
>>> re.findall(r"\w+ AAAA \w+$", s)
['foo2 AAAA bar2']

Also, note that list is a bad name for your variable, as it shadows built-in type. To access the last element of a list you could just use [-1] index:

>>> lst = [2, 3, 4]
>>> lst[-1]
4
SilentGhost
what if I came across a multiline string?
SDD
@SDD: it still would work fine
SilentGhost
This won't work for an input string like this: "foo bar AAAA foo2 AAAA bar2 bar3". Of course, we don't know if such a case is possible, we don't have enough information.
ΤΖΩΤΖΙΟΥ
+1  A: 

You can avoid the building of a list just by iterating over all matches and keeping the last match:

for match in re.finditer(r"\w+ AAAA \w+", "foo bar AAAA foo2 AAAA bar2"):
    pass

After this, match holds the last match, and works for all combinations of pattern and searched string. You might want to set match to None first, because if there's no match, match won't be set to any value.

ΤΖΩΤΖΙΟΥ