茅侃侃
您可能正在寻找import retext = "123 Lorem ipsum dolor sit amet, word WORD WoRd consetetur sadipscing elitr, sed diam 123"pattern = re.compile(r'\bword\b', re.IGNORECASE)for word in pattern.finditer(text): print(word.group(0))这会产生wordWORDWoRd\b是缩写形式(?:(?=\w)(?<!\w)|(?<=\w)(?!\w))其中读到(?=\w)(?<!\w) # positive lookahead making sure there's a word character coming # negative lookbehind making sure theres' n word characte preceding| # or(?<=\w)(?!\w) # the other way round所以,是的(?:(?=\w)(?<!\w)|(?<=\w)(?!\w))word(?:(?=\w)(?<!\w)|(?<=\w)(?!\w))会产生与上面相同的匹配,但似乎有点不可读。