简体   繁体   中英

Regex Python - inserting after special characters

I have a.txt file and I would like to split it in different rows using Python regex.

My.txt file looks like this:

a_lot(icl>how); CAT(CATADV) ; a_lot(icl>how); CAT(CATADV) ; a_lot(icl>how); CAT(CATADV) ;

and so on..

I would like to know how to do a Regex that says that after each "a_lot(icl>how); CAT(CATADV);"I want to start a new line.

So, I want my output to be:

"a_lot(icl>how); CAT(CATADV) ;
"a_lot(icl>how); CAT(CATADV) ;
"a_lot(icl>how); CAT(CATADV) ;

and so on..

Do you know how to do this?

Thanks

Try This

import re
s = """a_lot(icl>how) ; {CAT(CATADV)} ; a_lot(icl>how) ; {CAT(CATADV)} ; aare(iof>river>thing) ; {CAT(CATN),N(NP)} ; aarhus(iof>city>thing,equ>arhus) ; {CAT(CATN),N(NP)} ; abadan(iof>city>thing) ; {CAT(CATN),N(NP)} ; abandon(icl>leave>do,agt>person,obj>person ; {CAT(CATV),AUX(AVOIR),VAL1(GN)} ;"""
print(re.sub(r"\}\s*\;\s*", '}; \n', s))

Output

a_lot(icl>how) ; {CAT(CATADV)}; 
a_lot(icl>how) ; {CAT(CATADV)}; 
aare(iof>river>thing) ; {CAT(CATN),N(NP)}; 
aarhus(iof>city>thing,equ>arhus) ; {CAT(CATN),N(NP)}; 
abadan(iof>city>thing) ; {CAT(CATN),N(NP)}; 
abandon(icl>leave>do,agt>person,obj>person ; {CAT(CATV),AUX(AVOIR),VAL1(GN)};

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM