Removing Characters with boost::regex

Feb 24, 2010 at 6:17pm
I need to search a file with boost regex and remove what is found. I will not always remove what is found sometimes I will replace so I was trying to us regex_replace to just replace what was found with a " ", but it is not working?
here is my code:

application.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <boost/regex.hpp>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include "time.h"
using namespace std;



class application{
private:
	//Variables
	boost::regex expression;
	boost::smatch matches;
	string line;
	string pat;
	int lineNumber;
	char time[9];
	char date[9];
	
	//Functions
	void getExpression(){
		cout << "Expression: ";
		cin >> pat;
		try{
			expression = pat;
		}
		catch(boost::bad_expression){
			cout << pat << " is not a valid regular expression\n";
			exit(1);
		}
	}

	void boostMatch(){
		//Files to open
		//Input Files
		ifstream in("files/trff292010.csv");
			if(!in) cerr << "no file\n";
		//Output Files
			ofstream out("files/ORIGtrff292010.csv");
			ofstream newFile("files/NEWtrff292010.csv");
			ofstream record("files/record.dat");
		//time
			_strdate_s(date);
			_strtime_s(time);
			lineNumber = 0;
		
		while(in.peek() != EOF){
			getline(in, line, '\n');
			lineNumber++;
			out << line << "\n";
			if (regex_search(line, matches, expression)){
				for (int i = 0; i<matches.size(); ++i){
					record << "Date: "<< date << "Time: " << time << "\tmatches[" << i << "]: " << matches[i] << "\n\tLine Number: "<< lineNumber<< '\n\t\t' << line << '\n';
					boost::regex_replace(line, expression, " ");
					newFile << line << "\n";
				}
			}else{
				newFile << line << "\n";
			}
		}
	}

public:
	void run(){
		getExpression();
		boostMatch();
	}
};
Last edited on Feb 24, 2010 at 6:20pm
Feb 24, 2010 at 8:42pm
anyone know any good boost tutorials besides boost.org
Feb 24, 2010 at 9:31pm
Please post the input/output.

Also, does this really have to be done in C++? The sed command in UNIX/Linux is perfect for this.
Feb 24, 2010 at 9:40pm
whatever you input is what it outputs. An example of what it is used for is a list of names and a few have an asterisk like *alice. I want to search for the asterisk and make the output just alice. when I run it through this it shows it finds the asterisk and will log that but will not remove it. Yes this has to be done in C++ because this is a C++ exercise from my advanced C++ class. Not homework we are just supposed to get used to using boost for out upcoming big assignment
Feb 24, 2010 at 10:09pm
Please post the regex.
Feb 26, 2010 at 7:32pm
What do you mean post the regex?
Feb 26, 2010 at 8:16pm
Line 56, shouldn't that be:

std::string new_string = boost::regex_replace(line, expression, " ");

As it is, you are discarding the result of the regex_replace().
Topic archived. No new replies allowed.