remove line comment from file by using regex

Abdullah

I used the pattern below to remove comment lines from file, and succeed it in Visual Studio Editor. However, the same pattern didn't work with C++ regex class.

std::regex pattern ("#.*\n");
fullText =  std::regex_replace (fullText,pattern,"");

Code above is a very brief part from the implementation: You can assume all text is read into fullText at once.

Actual results must remove all comment lines from file/string. Trailing comments can be ignored.

Sample file is .txt extension and has the text below:

# Initialization file..
# This file supports line comments, and does not support trailing comments.
# Text here is not case sensitive.
# White spaces are ignored in file processing. 
# Values are comma "," separated. 


Colmn,          Colmn,  
1,              0xFF,
2,              0xFF, 
3,              0xFF,
4,              0xFF,
5,              0xFF,

I assume all lines must be finished with \n, and I attempt to select all text between # and \n.

Thanks in advance for the any advice.

Wiktor Stribiżew

The point here is that . does not match carriage returns in the ECMAScript 5 compliant regex and \n pattern does not match CR chars while \r does.

You may fix the issue by using [\r\n]* at the end of the pattern:

std::regex pattern{"#.*[\r\n]*"};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Regex to remove line from css file

Using sed with regex to remove text from a file

Remove line from php file using sed

regex a line from file using python

Remove substring from each line of text file with regex

Remove line from file

Extract numbers from line not starting with comment symbol using regex

Remove binary data from html file using Java Regex

how to remove file/folder path from string using regex

How to remove folder names from a file path using regex in javascript

alter a line and remove tag using perl from xml file?

Remove a single line from a very large file using sed

How to remove characters from file names using command line?

How can I remove a line from a csv file using pandas?

Remove line from file in bash script using sed command

Remove a line from a file Python

RegEx to remove repeated start of line using TextWrangler

Remove star and empty line using regex

Comment from start of file to a pattern matched line using sed

Is there a regex, perl, or sed that can remove one line from a particular group of lines in a file?

How to remove line `v u` from a file when line `u v` already exists using unix command

Remove multiple occurrences of a string regex on a file, line by line

How to remove complete line from the string on basis of generic type using regex

How can I remove a specific character from multi line string using regex in python

Remove comment lines from a file

remove the filtered line from the file using scala fs2 file streaming

how remove python comment in js using regEx

Remove lines and variables in file using a regex (python)

Problem using head to remove last line of file