-
Notifications
You must be signed in to change notification settings - Fork 0
/
re2.c
138 lines (136 loc) · 3.86 KB
/
re2.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*===============================================================
* 文件名称:re2.c
* 创建日期:2019年02月18日
================================================================*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <regex.h>
#include <assert.h>
#include <string.h>
using namespace std;
int find_first(string input, string pattern, string &out){
regex_t reg;
regmatch_t pm[1];
int iret = 0;
out = "";
/*编译正则表达式*/
iret = regcomp(®, pattern.c_str(), REG_EXTENDED|REG_NEWLINE);
if (iret != 0){
return -1;
}
iret = regexec(®, input.c_str(), 1, pm, 0);
if (iret == REG_NOMATCH){
out = "";
iret = input.length();
}else if (iret != 0) {
return -2;
}else{
out = input.substr(pm[0].rm_so,pm[0].rm_eo-pm[0].rm_so);
iret = pm[0].rm_eo;
}
regfree(®);
return iret;
}
int find_first(char *buff, char *pattern, char *outdata){
regex_t reg;
regmatch_t pm[1];
int status = 0;
/*编译正则表达式*/
status = regcomp(®, pattern, REG_EXTENDED|REG_NEWLINE); //扩展正则表达式和识别换行符
if (status != 0){ //成功返回0
return -1;
}
status = regexec(®, buff, 1, pm, 0);
if (status == REG_NOMATCH){
printf("no match!\n");
status = -1;
}
else if (status != 0) {
return -2;
}
else if (status == 0) {
int i, j;
for (i = pm[0].rm_so, j = 0; i < pm[0].rm_eo; i++, j++) {
outdata[j] = buff[i];
}
outdata[i] = '\0';
}
regfree(®);
return status;
}
int find_all(char *buff, char *pattern, char result[][20]){ //返回匹配个数
regex_t reg;
regmatch_t pm[1];
int status = 0;
char * p = buff;
int count = 0;
/*编译正则表达式*/
status = regcomp(®, pattern, REG_EXTENDED|REG_NEWLINE); //扩展正则表达式和识别换行符
if (status != 0){ //成功返回0
return -1;
}
int i = 0, j, k;
while((status = regexec(®, p, 1, pm, 0)) == 0) {
for(j = pm[0].rm_so, k = 0; j < pm[0].rm_eo; j++) {
result[i][k++] = p[j];
}
result[i][k] = '\0';
i++;
p += pm[0].rm_eo;
count++;
if (*p == '\0') break;
}
regfree(®);
return count;
}
int print_file(const char *file_name, const char *pattern)
{
regex_t reg;
regmatch_t pm[1];
int status = 0;
int count = 0;
FILE *fp = fopen(file_name, "r+");
assert(fp);
char buff[1024] = {0};
char output[1024] = {0};
/*编译正则表达式*/
status = regcomp(®, pattern, REG_EXTENDED|REG_NEWLINE); //扩展正则表达式和识别换行符
assert(status == 0);
while(fgets(buff, sizeof(buff), fp)) { //循环读取文件
char * p = buff;
while(1) {
status = regexec(®, p, 1, pm, 0);
if (status == 0) { //匹配成功
count++;
strncpy(output, p + pm[0].rm_so, pm[0].rm_eo - pm[0].rm_so);
cout<<"匹配:"<<output<<endl;
p += pm[0].rm_eo;
}
else {
break;
}
}
}
regfree(®);
return count;
}
int main()
{
char result[20][20] = {0};
char buf[] = "1231a4568b789c234";
char pattern[] = "[0-9]{3}";
char resultfirst[20] = {0};
find_first(buf, pattern, resultfirst);
cout<<strlen(resultfirst) << ":" << resultfirst<<endl;
cout << "***************************" <<endl;
int count = find_all(buf, pattern, result);
for (int i = 0; i < count; i++) {
cout<<"result:"<<"i="<<i+1<<"----"<<result[i]<<endl;
}
cout << "***************************" <<endl;
count = print_file("test.txt", "[0-9]{5}");
cout<<"匹配的个数:"<<count<<endl;
return 0;
}