I am new to regex. I have read various tutorials, still I have failed to run my simple codes.
My files are organized such as "c1c2c4_aa_1"
, "c1c2c3_aa_2"
, "c1c2c8_aa_3"
, "c1c3c4_aa_4"
, ... "c1c2c4_bb_41"
, "c1c8c9_cc_58"
, "c1c3c11_aa_19"
I want to find all those ones that includes "aa"
(such as "c1c2c3_aa_3"
) and convert them to "c1c2c4_zz_3"
So I want the last number and the first string before "_" remains fixed, but change the "aa" in the middle.
"c1", "c2", "c3" are some conditions. Also, the last numbers are quite random, so I do not know them to define them.
I am interested in using regex.
I tried this:
con_list1 = ["c1", "c2", ... "c8"]
con_list2 = ["c1", "c2", ... "c11"]
con_list3 = ["c1", "c2", ... "c10"]
for con1 in con_list1:
for con2 in con_list2:
for con3 in con_list3:
if(os.path.exists("./" + con1 + con2 + con3 + "_aa(.*)")):
os.rename("./" + con1 + con2 + con3 + "_aa(.*)", "./" + con1 + con2 + con3 + "_zz(.*)")
I want the last number corresponding to the file that I rename remains fixed:
"c1c2c3_aa_3" -> "c1c2c3_zz_3" "c1c2c3_aa_13" -> "c1c2c3_zz_13"
I am also interested in using regex and (.*) in the right way.
However, the above code seems not working.
I appreciate to help to implement this code.