i would like to substitute the below lines to,
ABC+::A1
CDE+:A1A1A1
Expected Output:
ABC+::A1
TIM+++2020:11:21:12:51+ZZZ
ABC++ADDEDTIME
CDE+:A1A1A1
I would like to add the following lines between the above two lines. Please note that from the above lines, A1/A1A1A1 is a variable will keep changing but the first three alpha character remains the same. Hence no matter what is mentioned after the colon (+::), the below two lines must be added,
Lines to be added
TIM+++2020:11:21:12:51+ZZZ
ABC++ADDEDTIME
We could make good use of re like this shown below:
import re
regex = r"^(ABC\+::[A-Z0-9]+\n)(?=^CDE\+:[A-Z0-9]+)"
test_str = ("ABC+::A1\n"
"CDE+:A1A1A1")
subst = "\\1TIM+++2020:11:21:12:51+ZZZ\\nABC++ADDEDTIME\\n"
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print(result)
Basically, the regex pattern matches ABC+::ALPHANUM{1, or more} followed by CDE+:ALPHANUM{1, or more} and substitutes the first match in brackets back, and adds line breaks and the new strings after it.
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.