Find all the occurences of a string in an imperfect text
<p>I am trying to find a string within a long text extracted from a PDF file, and get the string's position in the text, and then return 100 words before the string and 100 after.
The problem is that the extraction is not perfect, so I am having a problem like this:</p>

<p>The query string is "test text"</p>

<p>The text may look like:</p>

<blockquote>
  <p>This is atest textwith a problem</p>
</blockquote>

<p>as you can see the word "test" is joined with the letter "a" and the word "text" is joined with the word "with"</p>

<p>So the only function is working with me is __contains __ which doesn't return the position of the word.</p>

<p>Any ideas to find all the occurences of a word in such a text with their postions?</p>

<p>Thank you very much</p>

<p>You did not specify all your requirements but this works for your current problem. The program prints out <code>9 and 42</code>, which are the beginning of two occurrences of the <code>test text</code>.</p>

<pre><code>import re
filt = re.compile("test text")

for match in filt.finditer('This is atest textwith a problem. another test text'):
    print match.start()
</code></pre>

python