Automatically generating a regular expression
<p>I want the following function:</p>

<pre><code>def get_pattern_and_replacement(the_input, output):
    """
    Given the_input and output returns the pattern for matching more general case of the_input and a template string for generating the desired output.

    &gt;&gt;&gt; get_pattern_and_replacement("You're not being nice to me.", "I want to be treated nicely.")
    ("You're not being (?P&lt;word&gt;\w+) to me.", "I want to be treated {{ word }}ly.")
    &gt;&gt;&gt; get_pattern_and_replacement("You're not meeting my needs.", "I want my needs met.")
    ("You're not meeting my (?P&lt;word&gt;\w+).", "I want my {{ word }} met.")
    """
</code></pre>

<p>This is for a program to transform undesired text into desired text. </p>

<p>With help from Stackoverflow users my function is now:</p>

<pre><code>def flatten(nested_list):
    return [item for sublist in nested_list for item in sublist]

def get_pattern_and_replacement(the_input, output):
    """
    Given the_input and output returns the pattern for matching more general case of the_input and a template string for generating the desired output.

    &gt;&gt;&gt; get_pattern_and_replacement("You're not being nice to me.", "I want to be treated nicely.")
    ("You're not being (?P&lt;word&gt;\w+) to me.", "I want to be treated {{ word }}ly.")
    &gt;&gt;&gt; get_pattern_and_replacement("You're not meeting my needs.", "I want my needs met.")
    ("You're not meeting my (?P&lt;word&gt;\w+).", "I want my {{ word }} met.")
    """
    input_set = set(flatten([[the_input[i: i + j] for i in range(len(the_input) - j) if not ' ' in the_input[i: i + j]] for j in range(3, 12)]))
    output_set = set(flatten([[output[i: i + j] for i in range(len(the_input) - j) if not ' ' in output[i: i + j]] for j in range(3, 12)]))

    intersection = input_set &amp; output_set
    intersection = list(intersection)
    intersection = sorted(intersection, key=lambda x: len(x))[::-1]
    print intersection
    pattern = the_input.replace(intersection[0], '(?P&lt;word&gt;\w+)')
    replacement = output.replace(intersection[0], '{{ word }}')
    return (pattern, replacement)
</code></pre>
