Remove one-line comments containing Japanese from source code in Python

This article uses Python3.

code

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)

output

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

Remove one-line comments containing Japanese from source code in Python
Install python from source
Japanese output in Python
[Python] Django Source Code Reading View Starting from Zero ①
Get YouTube Comments in Python
I wrote python in Japanese
Install ansible from source code
OCR from PDF in Python
Stop Omxplayer from Python code
Generate QR code in Python
I understand Python in Japanese!
Character code learned in Python
Always check PEP8 while editing Python source code in Emacs
Get Japanese synonyms in Python
[Python] Generate QR code in memory
Install Python from source with Ansible
Automatically format Python code in Vim
Extract text from images in Python
Write selenium test code in python
Execute Python code from C # GUI
[Python] Read the Flask source code
Code tests around time in Python
How to handle Japanese in Python
Extract strings from files in Python
Detect Japanese characters from images using Google's Cloud Vision API in Python
Interrupt processing flow seen in source code
View the implementation source code in iPython
Get exchange rates from open exchange rates in Python
Getting Python source code metrics using radon
Download images from URL list in Python
Get battery level from SwitchBot in Python
Flow from source code to creating executable
Generate a class from a string in Python
Generate C language from S-expressions in Python
Get the EDINET code list in Python
Convert from Markdown to HTML in Python
Get Precipitation Probability from XML in Python
[Note] Execute Python code from Excel (xlwings)
Syntax highlighting source code in PowerPoint / Keynote
Install PostgreSQL from source code on CentOS
Notes on using code formatter in Python
Get metric history from MLflow in Python
Enter Japanese comments in Blender's text editor
Algorithm Introduction Implement 4 types of sorting in Python from pseudo code of 3rd edition
Difference in writing method to read external source code between Ruby and Python