home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
i18nv104.zip
/
SAMPLE
/
REGEX
/
REGEX.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-02-13
|
6KB
|
171 lines
#include <wchar.h>
#include <locale.h>
#include <regex.h>
#define MAX_STR_SIZE 256
main(void)
{
regex_t reg; /* regular expression status buffer */
int status, /* regexec status */
error = 0; /* error flag */
char pattern[MAX_STR_SIZE], /* pattern to look for */
test_string[MAX_STR_SIZE]; /* test string */
printf("%s\n", setlocale(LC_ALL, ""));
strcpy(pattern,"m");
strcpy(test_string,"this is my test string");
if(error = regcomp(®, pattern, REG_EXTENDED)) {
printf("error: %d\n", error);
}
else {
status = regexec(®, test_string, 0, NULL, 0);
if (status == 0)
printf("MATCH! : %s <- pattern found in -> %s\n", pattern,
test_string);
else
printf("NO MATCH! : %s <- pattern not found in -> %s\n",
pattern, test_string);
}
regfree(®);
strcpy(pattern,"^match this$");
strcpy(test_string,"match this");
if(error = regcomp(®, pattern, REG_EXTENDED)) {
printf("error: %d\n", error);
}
else {
status = regexec(®, test_string, 0, NULL, 0);
if (status == 0)
printf("MATCH! : %s <- pattern found in -> %s\n",
pattern, test_string);
else
printf("NO MATCH! : %s <- pattern not found in -> %s\n",
pattern, test_string);
}
regfree(®);
strcpy(pattern,"match this");
strcpy(test_string,"try to match this");
if(error = regcomp(®, pattern, REG_EXTENDED)) {
printf("error: %d\n", error);
}
else {
status = regexec(®, test_string, 0, NULL, 0);
if (status == 0)
printf("MATCH! : %s <- pattern found in -> %s\n",
pattern, test_string);
else
printf("NO MATCH! : %s <- pattern not found in -> %s\n",
pattern, test_string);
}
regfree(®);
strcpy(pattern,"no match here");
strcpy(test_string,"bdfgijklpqsuvwxyz");
if(error = regcomp(®, pattern, REG_EXTENDED)) {
printf("error: %d\n", error);
}
else {
status = regexec(®, test_string, 0, NULL, 0);
if (status == 0)
printf("MATCH! : %s <- pattern found in -> %s\n",
pattern, test_string);
else
printf("NO MATCH! : %s <- pattern not found in -> %s\n",
pattern, test_string);
}
regfree(®);
strcpy(pattern,"[[:alpha:]]");
strcpy(test_string,"123456789012345678901234567890");
if(error = regcomp(®, pattern, REG_EXTENDED)) {
printf("error: %d\n", error);
}
else {
status = regexec(®, test_string, 0, NULL, 0);
if (status == 0)
printf("MATCH! : %s <- pattern found in -> %s\n",
pattern, test_string);
else
printf("NO MATCH! : %s <- pattern not found in -> %s\n",
pattern, test_string);
}
regfree(®);
strcpy(pattern,"[[:digit:]]");
strcpy(test_string,"123456789012345678901234567890");
if(error = regcomp(®, pattern, REG_EXTENDED)) {
printf("error: %d\n", error);
}
else {
status = regexec(®, test_string, 0, NULL, 0);
if (status == 0)
printf("MATCH! : %s <- pattern found in -> %s\n",
pattern, test_string);
else
printf("NO MATCH! : %s <- pattern not found in -> %s\n",
pattern, test_string);
}
regfree(®);
strcpy(pattern,"[a-d]");
strcpy(test_string,"this is my test 123 string.");
if(error = regcomp(®, pattern, REG_EXTENDED)) {
printf("error: %d\n", error);
}
else {
status = regexec(®, test_string, 0, NULL, 0);
if (status == 0)
printf("MATCH! : %s <- pattern found in -> %s\n",
pattern, test_string);
else
printf("NO MATCH! : %s <- pattern not found in -> %s\n",
pattern, test_string);
}
regfree(®);
strcpy(pattern,"[a-e]");
strcpy(test_string,"this is my test 123 string.");
if(error = regcomp(®, pattern, REG_EXTENDED)) {
printf("error: %d\n", error);
}
else {
status = regexec(®, test_string, 0, NULL, 0);
if (status == 0)
printf("MATCH! : %s <- pattern found in -> %s\n",
pattern, test_string);
else
printf("NO MATCH! : %s <- pattern not found in -> %s\n",
pattern, test_string);
}
regfree(®);
strcpy(pattern,"[^x]");
strcpy(test_string,"you won't find one letter like that here");
if(error = regcomp(®, pattern, REG_EXTENDED)) {
printf("error: %d\n", error);
}
else {
status = regexec(®, test_string, 0, NULL, 0);
if (status == 0)
printf("MATCH! : %s <- pattern found in -> %s\n",
pattern, test_string);
else
printf("NO MATCH! : %s <- pattern not found in -> %s\n",
pattern, test_string);
}
regfree(®);
}