Skip to content

Instantly share code, notes, and snippets.

@FabianBartl
Last active February 7, 2024 16:49
Show Gist options
  • Save FabianBartl/e1419715a2507f9bff47ff15d5bac8f9 to your computer and use it in GitHub Desktop.
Save FabianBartl/e1419715a2507f9bff47ff15d5bac8f9 to your computer and use it in GitHub Desktop.
Regular expression to remove blank lines and single line comments in C/C++ files.
  • remove blank lines with ^\s*$\n
  • remove single line comments with *//.*$\n
  • remove inline comments with /\*.*\*/
  • replace tabs with \t by a single space
  • replace multiple spaces with + by a single space

all in one:

  • replace all above with (^\s*$\n)|( *//.*$\n)|(/\*.*\*/)|(\t) by spaces
  • reduce all the spaces with + to a single space
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment