I am trying to find and replace the second tab character in a string using regex.
booby = 'Joe Bloggs\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL\r\n'
This works fine:
re.sub(r'\t',r'###', booby)
This regular expression to find the second tab character doesnt work as expected:
re.sub(r'(\t[^\t]*)\t',r'###', booby)
Instead of matching and replacing the second tab I get this returned:
'###NULL\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL\r\n'
I've tried it with and without prepending r'', also I have confirmed the regular expression works on regex101.com
Edit : I have swapped the original regex for glibdud's superior one
You may be overthinking it a little.
>>> text = 'Joe Bloggs\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL\r\n'
>>> re.sub(r'(\t[^\t]*)\t', r'\1###', text, count=1)
'Joe Bloggs\tNULL###NULL\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL\r\n'
Simply match the first instance of a tab followed by any number of non-tabs followed by a tab, and replace it with everything but the final tab and whatever you want to replace it with.
>>> re.sub(r'^((?:(?!\t).)*\t(?:(?!\t).)*)\t',r'\1###', booby)
'Joe Bloggs\tNULL###NULL\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL\r\n'
You are almost there, add \\1
before ###
I provide another way to solve it because of the comments:
>>> booby.replace("\t", "###",2).replace("###", "\t",1)
'Joe Bloggs\tNULL###NULL\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL\r\n'
This is the shortest regex I could find :
import re
booby = 'Joe Bloggs\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL\r\n'
print re.sub(r'(\t.*?)\t', r'\1###', booby, 1)
It uses non-greedy .
to make sure it doesn't glob too many tabs. It outputs :
Joe Bloggs NULL###NULL NULL NULL NULL NULL NULL NULL
The regex might get ugly if you need it for other indices. You could use split
and join
for the general case :
n = 2
sep = '\t'
cells = booby.split(sep)
print sep.join(cells[:n]) + "###" + sep.join(cells[n:])
It outputs :
Joe Bloggs NULL###NULL NULL NULL NULL NULL NULL NULL
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.