這是正則表達式,可以幫助您提取所需文本:
(?:[^ ]+){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