-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregular.cc
executable file
·70 lines (58 loc) · 1.71 KB
/
regular.cc
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
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
const char * usage = ""
"Usage:\n"
" regular regular-expresion string\n"
"\n"
" Tells if \"string\" matches \"regular-expresion\".\n"
"\n"
" '^' and '$' characters are added at the beginning and\n"
" end of \"regular-expresion\" to force match of the entire\n"
" \"string\"\n"
"\n"
" To know more about regular expresions type \"man ed\"\n"
"Try:\n"
" bash> ./regular aaa aaa\n"
" bash> ./regular \"a*\" aaaa\n"
" bash> ./regular \"a*\" aaa\n"
" bash> ./regular \"a*\" aaaf\n"
" bash> ./regular \"a.*\" akjhkljh \n"
" bash> ./regular \"a.*\" jkjhkj\n"
" bash> ./regular \"a.*\" aaalklkjlk\n"
" bash> ./regular \".*\\..*\" kljhkljhlj.lkjhlkj\n"
" bash> ./regular \".*\\..*\" kljhkljhlj\n\n";
int
main(int argc, char ** argv)
{
if ( argc < 3 ) {
fprintf( stderr, "%s", usage );
exit( -1 );
}
const char * regularExp = argv[1];
const char * stringToMatch = argv[2];
/*
* Add ^ and $ at the beginning and end of regular expression
* to force match of the entire string. See "man ed".
*/
char regExpComplete[ 1024 ];
sprintf(regExpComplete, "^%s$", regularExp );
regex_t re;
int result = regcomp( &re, regExpComplete, REG_EXTENDED|REG_NOSUB);
if( result != 0 ) {
fprintf( stderr, "%s: Bad regular expresion \"%s\"\n",
argv[ 0 ], regExpComplete );
exit( -1 );
}
regmatch_t match;
result = regexec( &re, stringToMatch, 1, &match, 0 );
const char * matchResult = "MATCHES";
if ( result != 0 ) {
matchResult = "DOES NOT MATCH";
}
fprintf( stderr, "\t\"%s\" %s \"%s\"\n", matchResult,
regExpComplete, stringToMatch);
regfree(&re);
return 0;
}