Skip to content Skip to sidebar Skip to footer

Get The Actual Ending When Testing With .endswith(tuple)

I found a nice question where one can search for multiple endings of a string using: endswith(tuple) Check if string ends with one of the strings from a list My question is, how ca

Solution 1:

>>>s = "ERTYHGFYUUHGFREDFYAAAAAAAAAA">>>endings = ['AAAAA', 'AAAAAA', 'AAAAAAA', 'AAAAAAAA', 'AAAAAAAAA']>>>max([i for i in endings if s.endswith(i)],key=len)
'AAAAAAAAA'

Solution 2:

import re
str= "ERTYHGFYUUHGFREDFYAAAAAAAAAA"
endings = ['AAAAA', 'AAAAAA', 'AAAAAAA', 'AAAAAAAA', 'AAAAAAAAA']

printmax([i for i in endings if re.findall(i+r"$",str)],key=len)

Solution 3:

How about:

len(str) - len(str.rstrip('A'))

Solution 4:

str.endswith(tuple) is (currently) implemented as a simple loop over tuple, repeatedly re- running the match, any similarities between the endings are not taken into account.

In the example case, a regular expression should compile into an automaton that essentially runs in linear time:

regexp = '(' + '|'.join(
   re.escape(ending) for ending insorted(endings, key=len, reverse=True
) + ')$'

Edit 1: As pointed out correctly by Martijn Pieters, Python's re does not return the longest overall match, but for alternates only matches the first matching subexpression:

https://docs.python.org/2/library/re.html#module-re:

When one pattern completely matches, that branch is accepted. This means that once A matches, B will not be tested further, even if it would produce a longer overall match.

(emphasis mine)

Hence, unfortunately the need for sorting by length.

Note that this makes Python's re different from POSIX regular expressions, which match the longest overall match.

Post a Comment for "Get The Actual Ending When Testing With .endswith(tuple)"