pragma once include cstdio include string include cstring include rege

  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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#pragma once
#include <cstdio>
#include <string>
#include <cstring>
#include <regex>
#include <exception>
#include <pcre.h>
using namespace std;
class RegExp {
private:
pcre *re = NULL;
unsigned int max_matches = 0;
const char *pcre_error;
int pcre_error_offset;
int pcre_last_offset = 0;
int pcre_last_matches = 0;
const char *pcre_last_string = NULL;
unsigned int pcre_flags = 0;
unsigned int string_length;
int *result_vector = NULL;
RegExp *parseOptions(const char *options);
public:
typedef struct Match {
int start;
int end;
} _Match;
typedef std::function<string(RegExp *)> ReplaceCallback;
RegExp(const char *pattern, const char *options = NULL, unsigned int max_matches = 3);
RegExp *setPattern(const char *pattern, const char *options = NULL, unsigned int max_matches = 3);
int exec(const char *str, unsigned int len, int offset = -1);
int exec(string &str, int offset = -1);
int replace(string &str, string &out, const char *replace_str, unsigned int replace_length, ReplaceCallback callback, unsigned int limit = 0);
int replace(string &str, string &out, const char *replace_str, unsigned int replace_length, unsigned int limit = 0) {
return replace(str, out, replace_str, replace_length, NULL, limit);
}
inline int replace(string &str, string &out, string &replace_str, unsigned int limit = 0) {
return replace(str, out, replace_str.c_str(), replace_str.length(), limit);
}
inline int replace(string &str, string &out, const char *replace_str, unsigned int limit = 0) {
return replace(str, out, replace_str, strlen(replace_str), limit);
}
inline int replace(string &str, string &out, ReplaceCallback callback, unsigned int limit = 0) {
return replace(str, out, NULL, 0, callback, limit);
}
//
inline int replace(string &str, string &replace_str, unsigned int limit = 0) {
string tmp = str;
return replace(tmp, str, replace_str, limit);
}
inline int replace(string &str, const char *replace_str, unsigned int replace_length, unsigned int limit = 0) {
string tmp = str;
return replace(tmp, str, replace_str, replace_length, limit = 0);
}
inline int replace(string &str, const char *replace_str, unsigned int limit = 0) {
string tmp = str;
return replace(tmp, str, replace_str, strlen(replace_str), limit);
}
inline int replace(string &str, ReplaceCallback callback, unsigned int limit = 0) {
string tmp = str;
return replace(tmp, str, NULL, 0, callback, limit);
}
//
inline string creplace(string &str, const char *replace_str, unsigned int replace_length, ReplaceCallback callback, unsigned int limit = 0, unsigned int *replaced = NULL) {
string tmp = str;
int c = replace(str, tmp, replace_str, replace_length, callback, limit);
if (replaced)
*replaced = c;
return tmp;
}
inline string creplace(string &str, const char *replace_str, unsigned int replace_length, unsigned int limit = 0, unsigned int *replaced = NULL) {
return creplace(str, replace_str, replace_length, NULL, limit, replaced);
}
inline string creplace(string &str, const char *replace_str, unsigned int limit = 0, unsigned int *replaced = NULL) {
return creplace(str, replace_str, strlen(replace_str), NULL, limit, replaced);
}
inline string creplace(string &str, string &replace_str, unsigned int limit = 0, unsigned int *replaced = NULL) {
return creplace(str, replace_str.c_str(), replace_str.length(), NULL, limit, replaced);
}
inline string creplace(string &str, ReplaceCallback callback, unsigned int limit = 0, unsigned int *replaced = NULL) {
return creplace(str, NULL, 0, callback, limit, replaced);
}
inline int matched() { return pcre_last_matches; }
const string operator[](int n);
const string getString(int n, const char *ptr = NULL);
const string getString(int n, string &str);
const Match getMatch(int n);
RegExp *freePattern();
~RegExp();
};