This article uses Python3.
test.py
import re
#Line feed code
EOL = '\n'
#ASCII pattern
p_ac = '[!-~\\s]'
#Japanese(Non-ASCII)pattern
p_jp = '([^!-~\\s]|[ ])'
#1-line comment pattern
p_com = '//'
# p_com = '#'
#whole pattern
pattern = f'[ ]*{p_com}({p_ac}*{p_jp}+)+{p_ac}*$'
#The string you want to replace
s = '''1 aaa
2 bbb //Ai i u u e o o
3 //ka or ki ki ku ke ke ko ko
4 //Good
5 // uuu
6 print (aaa) //Show aaa
7 print (ccc) // print ccc
8
[EOD]'''
#Before replacement
print (s)
print ('----------------------')
n = ''
#Split line by line
ary = s.split(EOL)
#Replacement process
for l in ary:
#Delete target
r = re.sub(pattern, '', l)
n += r + EOL
#After replacement
print (n)
1 aaa
2 bbb //Ai i u u e o o
3 //ka or ki ki ku ke ke ko ko
4 //Good
5 // uuu
6 print (aaa) //Show aaa
7 print (ccc) // print ccc
8
[EOD]
----------------------
1 aaa
2 bbb
3
4
5 // uuu
6 print (aaa)
7 print (ccc) // print ccc
8
[EOD]
Recommended Posts