[Tutor] Question on regular expressions

Peter Otten __peter__ at web.de
Tue Feb 12 20:01:09 CET 2013


Marcin Mleczko wrote:

> given this kind of string:
> 
> "start SomeArbitraryAmountOfText start AnotherArbitraryAmountOfText end"
> 
> a search string like: r"start.*?end" would give me the entire string
> from the first "start" to "end" : "start SomeArbitraryAmountOfText start
> AnotherArbitraryAmountOfText end"
> 
> but I am interested only in the second part between the 2nd "start" and
> the "end": "start AnotherArbitraryAmountOfText end"
> 
> What would be best, most clever way to search for that?
> 
> Or even more general: how do I exlude always the text between the last
> "start" and the "end" tag assuming the entire text contains several
> "start" tags spaced by an arbitrary amount of text befor the "end" tag?

>>> s = "start SomeArbitraryAmountOfText start AnotherArbitraryAmountOfText 
end"

>>> [t[::-1] for t in re.compile("dne(.*?)trats").findall(s[::-1])]
[' AnotherArbitraryAmountOfText ']

Ok, I'm not serious about this one -- but how about

>>> parts = (t.partition("end") for t in s.split("start"))
>>> [left for left, mid, right in parts if mid]
[' AnotherArbitraryAmountOfText ']




More information about the Tutor mailing list