On 12/02/2013 17:43, Marcin Mleczko wrote:
Hello,
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?
Any ideas?
Thank you in advance. ;-)
Marcin
IMHO the best way is to use the rindex method to grab what you're after.
I don't do clever, it makes code too difficult to maintain. So how about.
>>> a="start SomeArbitraryAmountOfText start
AnotherArbitraryAmountOfText end"
>>> b="start "
>>> x=a.rindex(b)
>>> y=a.rindex(' end')
>>> a[x+len(b):y]
'AnotherArbitraryAmountOfText'
>>> c="garbage in, garbage out"
>>> x=c.rindex(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>>
--
Cheers.
Mark Lawrence
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor