Alan
Alan

Reputation: 3042

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

Upvotes: 1

Views: 107

Answers (2)

Ahasanul Haque
Ahasanul Haque

Reputation: 11134

This should help you. (View demo on regex101)

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

Sub with:

 \2\1

Upvotes: 1

n1c9
n1c9

Reputation: 2687

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

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

Upvotes: 0

Related Questions