import re
regex = re.compile(r"^([^.]+)\_([^.]+)\_([^.]+)\_([^.]+)\_([^.]+)\.fastq$", flags=re.MULTILINE)
test_str = ("A10_S65_L001_R1_001.fastq\n"
"A8_S49_L001_R2_001.fastq\n"
"B7_S42_L001_R1_001.fastq\n"
"C5_S27_L001_R2_001.fastq\n"
"F4_S22_L001_R1_001.fastq\n"
"G2_S7_L001_R2_001.fastq\n"
"H1_S165_L001_R1_001.fastq\n"
"A10_S65_L001_R2_001.fastq\n"
"A9_S57_L001_R1_001.fastq\n"
"B7_S42_L001_R2_001.fastq\n"
"C6_S35_L001_R1_001.fastq\n"
"F4_S22_L001_R2_001.fastq\n"
"G3_S15_L001_R1_001.fastq\n"
"H1_S165_L001_R2_001.fastq")
subst = "$1_$4.fastq"
result = regex.sub(subst, test_str)
if result:
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html