这是正则表达式,可以帮助您提取所需文本:
(?:[^ ]+){0,10}wolf(?: [^ ]+){0,10}
也是一个Python的例子应该像,虽然我不能现在测试它:
import re
t = "The domestic dog (Canis lupus familiaris or Canis familiaris) is a member of genus Canis (canines) that forms part of the wolf-like canids, and is the most widely abundant carnivore. The dog and the extant gray wolf are sister taxa, with modern wolves not closely related to the wolves that were first domesticated, which implies that the direct ancestor of the dog is extinct. The dog was the first domesticated species and has been selectively bred over millennia for various behaviors, sensory capabilities, and physical attributes"
m = re.search("(?:[^ ]+){0,10}wolf\s(?:[^ ]+){0,10}", t)
if m:
print (m.group(0))
当然这很简单,你是g遇到大量文本的性能问题。 – WombatPM