-
Notifications
You must be signed in to change notification settings - Fork 49
/
bin-libpcre-8.38.c
executable file
·164 lines (138 loc) · 7.35 KB
/
bin-libpcre-8.38.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* -*- Mode:C; Coding:us-ascii-unix; fill-column:132 -*- */
/**********************************************************************************************************************************/
/**
@file pcre_example.c
@author Mitch Richling <http://www.mitchr.me/>
@Copyright Copyright 1994,1998 by Mitch Richling. All rights reserved.
@brief UNIX regex tools@EOL
@Keywords UNIX regular expressions regex perl pcre
@Std ISOC POSIX.2 (IEEE Std 103.2) BSD4.3
@Tested
- Solaris 2.8
- MacOS X.2
- Linux (RH 7.3)
This is an example program intended to illustrate very basic use of the PCRE regular expression library. PCRE is NOT part of any
UNIX or language standard; however, it is commonly found on UNIX systems today, and it has a compatibility mode that supports the
standard interfaces.
The vast irregularities found in various UNIX favors with regard to regular expression support can make using regular expressions
frustrating. It can be less difficult, and safer, to simply carry around a regular expression library with you! PCRE is by far
the most popular, "alternate", regular expression library available today. It makes a fine choice for the budding UNIX
programmer unwilling to explore the vulgarities of some operating system vendor's regular expression library. If you are a C++
programmer, another good alternative may be found as part of the BOOST library.
Note: This program is very similar to the regex_example.c example found in this directory.
***********************************************************************************************************************************/
#include <pcre.h> /* PCRE lib NONE */
#include <stdio.h> /* I/O lib C89 */
#include <stdlib.h> /* Standard Lib C89 */
#include <string.h> /* Strings C89 */
/**********************************************************************************************************************************/
int main(int argc, char *argv[]) {
pcre *reCompiled;
pcre_extra *pcreExtra;
int pcreExecRet;
int subStrVec[30];
const char *pcreErrorStr;
int pcreErrorOffset;
char *aStrRegex;
char **aLineToMatch;
const char *psubStrMatchStr;
int j;
char *testStrings[] = { "This should match... hello",
"This could match... hello!",
"More than one hello.. hello",
"No chance of a match...",
NULL};
printf("lscan test binary file statically compiled with libpcre-8.38\n");
aStrRegex = "(.*)(hello)+";
printf("Regex to use: %s\n", aStrRegex);
// First, the regex string must be compiled.
reCompiled = pcre_compile(aStrRegex, 0, &pcreErrorStr, &pcreErrorOffset, NULL);
/* OPTIONS (second argument) (||'ed together) can be:
PCRE_ANCHORED -- Like adding ^ at start of pattern.
PCRE_CASELESS -- Like m//i
PCRE_DOLLAR_ENDONLY -- Make $ match end of string regardless of \n's
No Perl equivalent.
PCRE_DOTALL -- Makes . match newlins too. Like m//s
PCRE_EXTENDED -- Like m//x
PCRE_EXTRA --
PCRE_MULTILINE -- Like m//m
PCRE_UNGREEDY -- Set quantifiers to be ungreedy. Individual quantifiers
may be set to be greedy if they are followed by "?".
PCRE_UTF8 -- Work with UTF8 strings.
*/
// pcre_compile returns NULL on error, and sets pcreErrorOffset & pcreErrorStr
if(reCompiled == NULL) {
printf("ERROR: Could not compile '%s': %s\n", aStrRegex, pcreErrorStr);
exit(1);
} /* end if */
// Optimize the regex
pcreExtra = pcre_study(reCompiled, 0, &pcreErrorStr);
/* pcre_study() returns NULL for both errors and when it can not optimize the regex. The last argument is how one checks for
errors (it is NULL if everything works, and points to an error string otherwise. */
if(pcreErrorStr != NULL) {
printf("ERROR: Could not study '%s': %s\n", aStrRegex, pcreErrorStr);
exit(1);
} /* end if */
for(aLineToMatch=testStrings; *aLineToMatch != NULL; aLineToMatch++) {
printf("String: %s\n", *aLineToMatch);
printf(" %s\n", "0123456789012345678901234567890123456789");
printf(" %s\n", "0 1 2 3");
/* Try to find the regex in aLineToMatch, and report results. */
pcreExecRet = pcre_exec(reCompiled,
pcreExtra,
*aLineToMatch,
strlen(*aLineToMatch), // length of string
0, // Start looking at this point
0, // OPTIONS
subStrVec,
30); // Length of subStrVec
/* pcre_exec OPTIONS (||'ed together) can be:
PCRE_ANCHORED -- can be turned on at this time.
PCRE_NOTBOL
PCRE_NOTEOL
PCRE_NOTEMPTY */
// Report what happened in the pcre_exec call..
//printf("pcre_exec return: %d\n", pcreExecRet);
if(pcreExecRet < 0) { // Something bad happened..
switch(pcreExecRet) {
case PCRE_ERROR_NOMATCH : printf("String did not match the pattern\n"); break;
case PCRE_ERROR_NULL : printf("Something was null\n"); break;
case PCRE_ERROR_BADOPTION : printf("A bad option was passed\n"); break;
case PCRE_ERROR_BADMAGIC : printf("Magic number bad (compiled re corrupt?)\n"); break;
case PCRE_ERROR_UNKNOWN_NODE : printf("Something kooky in the compiled re\n"); break;
case PCRE_ERROR_NOMEMORY : printf("Ran out of memory\n"); break;
default : printf("Unknown error\n"); break;
} /* end switch */
} else {
printf("Result: We have a match!\n");
// At this point, rc contains the number of substring matches found...
if(pcreExecRet == 0) {
printf("But too many substrings were found to fit in subStrVec!\n");
// Set rc to the max number of substring matches possible.
pcreExecRet = 30 / 3;
} /* end if */
// Do it yourself way to get the first substring match (whole pattern):
// char subStrMatchStr[1024];
// int i, j
// for(j=0,i=subStrVec[0];i<subStrVec[1];i++,j++)
// subStrMatchStr[j] = (*aLineToMatch)[i];
// subStrMatchStr[subStrVec[1]-subStrVec[0]] = 0;
//printf("MATCHED SUBSTRING: '%s'\n", subStrMatchStr);
// PCRE contains a handy function to do the above for you:
for(j=0; j<pcreExecRet; j++) {
pcre_get_substring(*aLineToMatch, subStrVec, pcreExecRet, j, &(psubStrMatchStr));
printf("Match(%2d/%2d): (%2d,%2d): '%s'\n", j, pcreExecRet-1, subStrVec[j*2], subStrVec[j*2+1], psubStrMatchStr);
} /* end for */
// Free up the substring
pcre_free_substring(psubStrMatchStr);
} /* end if/else */
printf("\n");
} /* end for */
// Free up the regular expression.
pcre_free(reCompiled);
// Free up the EXTRA PCRE value (may be NULL at this point)
if(pcreExtra != NULL)
pcre_free(pcreExtra);
// We are all done..
return 0;
} /* end func main */