File: replace_demo.cc

package info (click to toggle)
libpcre%2B%2B 0.9.5-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,608 kB
  • ctags: 211
  • sloc: sh: 9,165; cpp: 835; makefile: 78
file content (37 lines) | stat: -rw-r--r-- 764 bytes parent folder | download | duplicates (5)
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
#include <string>
#include "../libpcre++/pcre++.h"

using namespace std;
using namespace pcrepp;

void usage();

int main(int argc, char *argv[]) {
  if(argc <3)
    usage();
  else {
    string search  = argv[1];
    string replace = argv[2];

    Pcre expression(search, "g");

    string input;

    cout << "> ";
    while(getline(cin, input)) {
      cout << expression.replace(input, replace) << endl;
      cout << "> ";
    }
  }
  return true;
}

void usage() {
  cout << endl
       << "Usage: replace <search-for> <replace-with>\n"
       << "search must be a regular expression.\n"
       << "Every line on STDIN will then searched\n"
       << "for 'search-for'. Every match will be replaced\n"
       << "with 'replace-with'." << endl;
  exit(-1);
}