 <p>The Python std lib includes a diff module, difflib. Here is a stab at your problem:</p> <pre><code>&gt;&gt;&gt; import difflib &gt;&gt;&gt; f1 = "111xxx222" &gt;&gt;&gt; f2 = "111yyy222" &gt;&gt;&gt; sm = difflib.SequenceMatcher(a=f1, b=f2) &gt;&gt;&gt; diff_re = '' &gt;&gt;&gt; for a,b,n in sm.get_matching_blocks(): ... if not n: continue ... if diff_re: diff_re += "(.*)" ... diff_re += f1[a:a+n] ... &gt;&gt;&gt; print diff_re '111(.*)222' </code></pre>