Alan Alan - 3 months ago 16
Python Question

regex - swap two phrases around

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 = re.compile('^(.*) \| (.*)$', re.IGNORECASE)
mytext = p.sub(r'\2\1', mytext)


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):

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


and should end up like:

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


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

Here is the full 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()

Answer

This should help you. (View demo on regex101)

(\S+)\s*\|\s*(.+)

Sub with:

 \2\1