I want to scan for the situation where a certain string ‘INC0459444’ is next to a date as in the following example
INC04594442024-06-05
If this is the case, I want to interpolate the text ‘tnullt’ between the INC string and the date. How to do that with re.sub?
2
If you are certain that the date will be in the YYYY-MM-DD format, you can make a regular expression that matches it.
import re
content = "INC04594442024-06-05"
pattern=r'(INCd{7})(d{4}-d{2}-d{2})'
replace = r'1tnullt2' # 1 and 2 are the capture group in parentheses
result = re.sub(pattern, replace, content)
print(result == 'INC0459444tnullt2024-06-05') # True
0