title: efficient way to get words before and after substring in text (python)
body: <p>I'm using regex to find occurrences of string patterns in a body of text. Once I find that the string pattern occurs, I want to get x words before and after the string as well (x could be as small as 4, but preferably ~10 if still as efficient).</p>
  
  <p>I am currently using regex to find all instances, but occasionally it will hang. Is there a more efficient way to solve this problem?</p>
  
  <p>This is the solution I currently have:</p>
  
  <pre><code>sub = r'(\w*)\W*(\w*)\W*(\w*)\W*(\w*)\W*(%s)\W*(\w*)\W*(\w*)\W*(\w*)\W*(\w*)' % result_string #refind string and get surrounding += 4 words
  surrounding_text = re.findall(sub, text)
  for found_text in surrounding_text:
    result_found.append(" ".join(map(str,found_text)))
  </code></pre>