最好的办法是去掉有时出现在维基百科引用文献开头的字母?
例如来自
a b c d Star Wars Episode III: Revenge of the Sith (DVD). 20th Century Fox. 2005.
至
Star Wars Episode III: Revenge of the Sith (DVD). 20th Century Fox. 2005.
我已经编写了一个可行的解决方案,但看起来很笨拙。我的版本使用“^(?:a (?:b (?:c )?)?)?”形式的正则表达式。什么是正确、快速的方法?
a = list('abcdefghijklmnopqrstuvwxyz')
reg = "^%s%s" % ( "".join(["(?:%s " %b for b in a]), ")?"*len(a) )
re.sub(reg, "", "a b c d Wikipedia Reference")
最佳答案
我可能会做这样的事情:
title = re.sub(r'^([a-z]\s)*', '', 'a b c d Wikipedia Reference')
它的作用与你在那里的作用相同。然而,就像 @joran-beasley 指出的那样,对于复杂的情况,您可能需要更聪明的东西。
关于python - 从字符串开头删除连续字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13737237/