How to Quickly Remove Code Comments in Embedded Projects
This guide explains practical methods—including specialized tools, custom scripts, and regular‑expression tricks in editors like VS Code and Notepad++—to efficiently strip //, /* */ and blank‑line comments from C/C++ source files for software copyright submissions.
Methods to Remove Comments from Embedded C Code
Three main approaches are commonly used:
Dedicated comment‑removal tools – Executable utilities can batch‑process files but may alter formatting.
Custom scripts – Writing a script (e.g., in Python) gives full control over which comment styles are stripped and how whitespace is handled.
Editor‑based regular expressions – Most code editors (VS Code, Notepad++) support regex replace across multiple files.
Regular‑expression patterns for C‑style comments
/\*{1,2}[\s\S]*?\*/ //[\s\S]*?
^\s*
These patterns match block comments ( /* … */), line comments ( // …) and empty lines respectively.
Batch removal with Notepad++
Open Find in Files (Ctrl+Shift+F).
Enter the desired regex in Find what (e.g., /\*{1,2}[\s\S]*?\*/) and enable Regular expression mode.
Specify file filters such as *.c;*.h and target directories (e.g., app, bsp, lib).
Click Replace in Files to strip matching comments from all selected files.
This procedure works for any number of source folders and requires only basic familiarity with regular expressions.
Python script example
A minimal script can read each source file, apply the three regexes, and write the cleaned content back. Example skeleton:
import re, pathlib
pattern_block = re.compile(r'/\*{1,2}[\s\S]*?\*/')
pattern_line = re.compile(r'//[\s\S]*?
')
pattern_blank = re.compile(r'^\s*
', re.MULTILINE)
for path in pathlib.Path('src').rglob('*.c'):
text = path.read_text(encoding='utf-8')
text = pattern_block.sub('', text)
text = pattern_line.sub('', text)
text = pattern_blank.sub('', text)
path.write_text(text, encoding='utf-8')Adjust the directory and file extensions as needed.
Removing comments is a safer alternative to deleting entire repositories when preparing code for copyright submission.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
