Q:

Extract words preceding and following search terms

Suppose I have a text like the following. 

The City of New York often called New York City or simply New York is
  the most populous city in the United States.  With an estimated
  population of 8537673 distributed over a land area of about 3026
  square miles (784 km2) New York City is also the most densely
  populated major city in the United States. 

I want to locate the n words preceding and following occurrences of the a search term.  For example, n=3 and search term="New York", then 
1st occurrence:

words preceding = {The, city, of}
words following = {often, called, New}

2nd occurrence:

words preceding = {York, often, called}
words following = {City, or, simply}

3rd occurence:

words preceding = {City, or, simply}
words following = {is, the, most}

4th occurrence:

words preceding = {miles, 784, km2}
words following = {City, is, also}

How can I do this using regex?  I found a similar question here Extract words surrounding a search word but it does not consider multiple occurrences of the search term. 
Attempts:
def search(text,n): 
word = r"\W*([\w]+)" 
groups = re.search(r'{}\W*{}{}'.format(wordn,'place',wordn), text).groups() return groups[:n],groups[n:]

A:

You need to use a positive lookahead assertion in order to handle overlapping matches:
re.findall(r"((?:\w+\W+){3})(?=New York((?:\W+\w+){3}))", t)

Result:
[('The City of ', ' often called New'),
 ('York often called ', ' City or simply'),
 ('City or simply ', ' is the most'),
 ('miles (784 km2) ', ' City is also')]

