Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex re.sub list in a file

Tags:

python

regex

I have a text list that will re.sub fine with some like: re.sub('0000', '1111',data).

A replace pattern ^(.{4})(.{4})(.{3})(.{3}) with \1\4\2\3 for one input in the shell works fine also. However, my attempt to use this pattern in a list gives me an undesirable result on the first row and never replace the latters. What am I missing here?

"0000-22N-06W-01"
"0000-22N-06W-02"
"0000-22N-06W-03"
"0000-22N-06W-04"

import re
o = open("output.txt","w")
data = open("input.txt").read()
o.write(re.sub(r'^(.{4})(.{4})(.{3})(.{3})', r'\1\4\2\3',data))
o.close()
like image 333
George Avatar asked Sep 21 '13 17:09

George


People also ask

Does re sub replace all occurrences?

sub() Replace matching substrings with a new string for all occurrences, or a specified number.

Is sub () A regex function?

sub() function belongs to the Regular Expressions ( re ) module in Python. It returns a string where all matching occurrences of the specified pattern are replaced by the replace string. To use this function, we need to import the re module first.

What is R IN RE sub?

The r prefix is part of the string syntax. With r , Python doesn't interpret backslash sequences such as \n , \t etc inside the quotes. Without r , you'd have to type each backslash twice in order to pass it to re. sub . r'\]\n'

What is sub in regex?

The regex function re. sub(P, R, S) replaces all occurrences of the pattern P with the replacement R in string S . It returns a new string. For example, if you call re. sub('a', 'b', 'aabb') , the result will be the new string 'bbbb' with all characters 'a' replaced by 'b' .


1 Answers

"0000-22N-06W-01"
"0000-22N-06W-02"
"0000-22N-06W-03"
"0000-22N-06W-04"

import re
output = open("output.txt","w")
input = open("input.txt")

for line in input:
    output.write(re.sub(r'^(.{4})-(.{3})-(.{3})-(.{2})$', r'\1-\4-\2-\3', line))

input.close()
output.close()

NOTE: If you actually have " in your data then you should change your regular expression to this one:

^"(.{4})-(.{4})-(.{3})-(.{3})"$

Regex101 Demo

like image 159
Ibrahim Najjar Avatar answered Sep 21 '22 18:09

Ibrahim Najjar