写了一个删除注释的python脚本,能够通过正则匹配:
文档注释:/** */
区块注释:/* */
单行注释://
注:无法识别字符串内的注释
dyc_raw.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| __author__ = 'xana' import re, sys def rename(old, str='dyc'): new = old.split('.') return new[0]+'_'+str+'.'+new[1] def success(str): str = re.sub(r'//[\s\S]*?\n', '\n', str) # //(.*) str = re.sub(r'/\*{1,2}[\s\S]*?\*/', '', str) return str def failed(): pass with open(sys.argv[1], 'r') as fi: with open(rename(sys.argv[1]), 'w') as fo: fo.write(success(fi.read()))
|
在命令行移动到目标文件路径下后
1 2 3 4 5 6
| . ├── a │ └── a.js ├── dyc.py ├── fuck.txt └── test.js
|
执行python dyc.py fuck.txt
1 2 3 4 5 6 7
| . ├── a │ └── a.js ├── dyc.py ├── fuck.txt ├── fuck_dyc.txt └── test.js
|
即可生成所需的dyc文件
附带烧脑的测试代码·真:
test.js
1 2 3 4 5 6 7 8 9 10 11 12
| * this is a document comment * Created by xana on 15/10/26. */ function test(e){ // this is an area comment */ var str = 'please//don\'t/*delete', str2 = 'me"in*/your//test'; return e; }
|
test_out.js
1 2 3 4
| function test(e){ var str = 'please//don\'t/*delete', str2 = 'me"in*/your//test'; return e; }
|