ハックが、収量使用しています:
import string
li_test = [
("My string is #not very beautiful","My string is"),
("Are you 9 years old?","Are you "),
("this is the last example","this is the last "),
]
tolerated = string.ascii_letters
def rstrip_(s_in):
last = None
for char in s_in:
if char in tolerated:
last = char
yield char
elif char == ' ':
if last == ' ':
raise StopIteration()
last = char
yield char
else:
raise StopIteration()
for input_, exp in li_test:
got = "".join(rstrip_(input_))
msg = ":%s:<>:%s:" % (exp, got)
print (":%s:=>:%s:" % (input_, got))
#cheating a bit because I dunno if the last space is wanted.
assert exp.rstrip() == got.rstrip(), msg
出力:
:My string is #not very beautiful:=>:My string is :
:Are you 9 years old?:=>:Are you :
:this is the last example:=>:this is the last :
そして、はい、私は第二の機能で全体を包んで参加している必要がありますがそこの文字...
あなたはtを意味しましたか?空白も省略しますか? – inbinder
私は、文字または単一のスペースではない文字の後に続くものはすべて省略することを意味します。以前のものはまったく同じです。その文字の前に空白があり、その文字を削除する方法が分かっている場合、それは追加ボーナスです。 – user
あなたはグループをキャプチャすることでこれを行うことができます... – deweyredman