regex - swap two phrases around
<p>Python 3. Each line is constructed of a piece of text, then a pipe symbol, then a second piece of text.
I want to swap the two pieces of text around and remove the pipe.
This is the code so far:</p>

<pre><code>p = re.compile('^(.*) \| (.*)$', re.IGNORECASE)
mytext = p.sub(r'\2\1', mytext)
</code></pre>

<p>Yet for some reason that I can't work out, it is not matching.
A sample of the text it should be matching is (ironically):</p>

<pre><code>(https://www.youtube.com/watch?v=NIKdKCQnbNo) | [Regular Expressions 101 - YouTube]
</code></pre>

<p>and should end up like:</p>

<pre><code>[The Field Expedient Pump Drill - YouTube](https://www.youtube.com/watch?v=4QDXUxTrlRw)
</code></pre>

<p>(in other words, the code is formatting the links into the format expected of a markdown converter).</p>

<p>Here is the full code:</p>

<pre><code>#! /usr/bin/env python3

import re, os

def create_text(myinputfile):
    with open(myinputfile, 'r', encoding='utf-8') as infile:
        mytext = infile.read()
    return mytext

def reg_replace(mytext):
    p = re.compile('^(.*) \| (.*)$', re.IGNORECASE)
    mytext = p.sub(r'\2\1', mytext)
return mytext

def write_out(mytext, myfinalfile):
    with open(myfinalfile, 'w') as myoutfile:
        myoutfile.write(mytext)

def main():
    mytext = create_text('out.md')
    mytext = reg_replace(mytext)
    write_out(mytext, 'out1.md')
    os.rename("out.md", "out_original.md")
    os.rename("out1.md", "out.md")

main()
</code></pre>

<p>sorry if I'm missing something here, but why not just use <code>re.match</code> with groups instead of <code>re.sub</code>?:</p>

<pre><code>import re

p = re.compile('^(.*) \| (.*)$', re.IGNORECASE)
sample = "(https://www.youtube.com/watch?v=NIKdKCQnbNo) | [Regular Expressions 101 - YouTube]"

matches = p.search(sample)

new_string = "{0}{1}".format(matches.group(2), matches.group(1))
print(new_string)
&gt;&gt;&gt; [Regular Expressions 101 - YouTube](https://www.youtube.com/watch?v=NIKdKCQnbNo)
</code></pre>

python regex python-3.x