aboutsummaryrefslogtreecommitdiff
path: root/regexptest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'regexptest.cpp')
-rw-r--r--regexptest.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/regexptest.cpp b/regexptest.cpp
new file mode 100644
index 0000000..01597be
--- /dev/null
+++ b/regexptest.cpp
@@ -0,0 +1,47 @@
1#include <stdio.h>
2#include <regex.h>
3#include <string>
4
5int test( const char *str, const char *sRegExp )
6{
7 printf("Compiling: %s\n", sRegExp );
8 regex_t r;
9 if( regcomp( &r, sRegExp, REG_EXTENDED|REG_NEWLINE ) )
10 {
11 printf("Error compiling regular expression.\n");
12 return 0;
13 }
14
15 printf("Compiled, %d sub expressions.\n", r.re_nsub );
16
17 int nMatch = r.re_nsub+1;
18 regmatch_t *match = new regmatch_t[nMatch];
19 if( regexec( &r, str, nMatch, match, 0 ) )
20 {
21 printf("Regular expression did not match.\n");
22 return 0;
23 }
24
25 printf("Match!\nSubstrings:\n");
26 for( int j = 0; j < nMatch; j++ )
27 {
28 printf(" %d: (%d-%d) %s\n",
29 j,
30 match[j].rm_so, match[j].rm_eo,
31 std::string(str+match[j].rm_so, match[j].rm_eo-match[j].rm_so ).c_str()
32 );
33 }
34
35 delete[] match;
36 regfree( &r );
37}
38
39int main( int argc, char *argv[] )
40{
41 printf("Regular expression test:\n\n");
42
43 test( argv[1], argv[2] );
44
45 return 0;
46}
47