[Tutor] how to match regular expression from right to left

Kent Johnson kent37 at tds.net
Sun Sep 16 16:01:54 CEST 2007


王超 wrote:
> hi,
> 
> I want to match the regular expression from right to left, such as:
> 
> TAG_pattern = re.compile(r"(us::.*) .*(1002|1003).*$")
> TAG_pattern.search(line)
> 
> Does the search method support this?

What do you mean match from right to left? If you mean, match an re that 
is anchored at the end of the string, then sure, $ is supported. If you 
mean, find the right-most match of several (similar to the rfind method 
of a string) then no, it is not directly supported. You could use 
re.findall() and chose the last match but if the matches can overlap 
this may not give the correct result.

Kent


More information about the Tutor mailing list