-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcregex.cc
More file actions
38 lines (34 loc) · 1.17 KB
/
cregex.cc
File metadata and controls
38 lines (34 loc) · 1.17 KB
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
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
#define MAX_MATCHES 20 //The maximum number of matches allowed in a single string
void match(regex_t *pexp, char *sz) {
regmatch_t matches[MAX_MATCHES]; //A list of the matches in the string (a list of 1)
//Compare the string to the expression
//regexec() returns 0 on match, otherwise REG_NOMATCH
if (regexec(pexp, sz, MAX_MATCHES, matches, 0) == 0) {
int i = 0;
while(matches[i].rm_so != -1) {
printf("\"%s\" matches characters %d - %d\n", sz, matches[i].rm_so, matches[i].rm_eo);
++i;
}
} else {
printf("\"%s\" does not match\n", sz);
}
}
int main() {
int rv;
regex_t exp; //Our compiled expression
//1. Compile our expression.
//Our regex is "-?[0-9]+(\\.[0-9]+)?". I will explain this later.
//REG_EXTENDED is so that we can use Extended regular expressions
rv = regcomp(&exp, "^(https?://)www(\\.www\\.)(aa)", REG_EXTENDED);
if (rv != 0) {
printf("regcomp failed with %d\n", rv);
}
//2. Now run some tests on it
match(&exp, "https://www.www.aa");
//3. Free it
regfree(&exp);
return 0;
}