words = ["Hello", "Hello-World", "Hello_", "Hello1", "_hello_", "hello:",
"hello:)" ]
import re
for word in words:
print re.findall(r'\b(?!_)[\w-]+(?!:)\b', word)
\b Matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of alphanumeric or underscore characters, so the end of a word is indicated by whitespace or a non-alphanumeric, non-underscore character.
在您所要求的内容中仍然有相当多的模糊性,但是这里是您给出的示例集的另一个解决方案,在此之前fiddle
您可以添加前导和尾随
\b
。在输出:
^{pr2}$来自http://docs.python.org/2/library/re.html
相关问题 更多 >
编程相关推荐