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()
sub() Replace matching substrings with a new string for all occurrences, or a specified number.
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.
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'
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' .
"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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With