The following subroutines are
available for use with internationalized regular expressions.
| regcomp | Compiles a specified basic or extended regular expression into an executable string. |
| regexec | Compares a null-terminated string with a compiled basic or extended regular expression that must have been previously compiled by a call to the regcomp subroutine. |
| regerror | Provides a mapping from error codes returned by the regcomp and regexec subroutines to printable strings. |
| regfree | Frees any memory allocated by the regcomp subroutine associated with the compiled basic or extended regular expression. The expression is no longer treated as a compiled basic or extended regular expression after it is given to the regfree subroutine. |
| fnmatch | Checks a specified string to see if it matches a specified pattern. You can use the fnmatch subroutine in an application that reads a dictionary to find which entries match a given pattern. You also can use the fnmatch subroutine to match pathnames to patterns. |
#include <locale.h>
#include <regex.h>
#define BUFSIZE 256
main()
{
char *p;
char *pattern[] = {
"hello[0-9]*",
"1234"
};
char *string = "this is a test string hello112 and this is test";
/* This is the source string for matching */
int retval;
regex_t re;
char buf[BUFSIZE];
int i;
setlocale(LC_ALL, "");
for(i = 0;i <2; i++){
retval = match(string, pattern[i], &re);
if(retval == 0){
printf("Match found \n");
}else{
regerror(retval, &re, buf, BUFSIZE);
printf("error = %s\n", buf);
}
}
regfree( &re);
}
int match(char *string, char *pattern, regex_t *re)
{
int status;
if((status=regcomp( re, pattern, REG_EXTENDED))!= 0)
return(status);
status = regexec( re, string, 0, NULL, 0);
return(status);
}
#include <locale.h>
#include <regex.h>
#define BUFSIZE 256
main()
{
char *p;
char *pattern = "[0-9]";
char *string = "Today is 11 Feb 1992 ";
int retval;
regex_t re;
char buf[BUFSIZE];
regmatch_t pmatch[100];
int status;
char *ps;
int eflag;
setlocale(LC_ALL, "");
/* Compile the pattern */
if((status = regcomp( &re, pattern, REG_EXTENDED))!= 0){
regerror(status, &re, buf, 120);
exit(2);
}
ps = string;
printf("String to match=%s\n", ps);
eflag = 0;
/* extract all the matches */
while( status = regexec( &re, ps, 1, pmatch, eflag)== 0){
printf("match found at: %d, string=%s\n",
pmatch[0].rm_so, ps +pmatch[0].rm_so);
ps += pmatch[0].rm_eo;
printf("\nNEXTString to match=%s\n", ps);
eflag = REG_NOTBOL;
}
regfree( &re);
}
#include <locale.h>
#include <fnmatch.h>
#include <sys/dir.h>
main(int argc, char *argv[] )
{
char *pattern;
DIR *dir;
struct dirent *entry;
int ret;
setlocale(LC_ALL, "");
dir = opendir(".");
pattern = argv[1];
if(dir != NULL){
while( (entry = readdir(dir)) != NULL){
ret = fnmatch(pattern, entry->d_name,
FNM_PATHNAME|FNM_PERIOD);
if(ret == 0){
printf("%s\n", entry->d_name);
}else if(ret == FNM_NOMATCH){
continue ;
}else{
printf("error file=%s\n",
entry->d_name);
}
}
closedir(dir);
}
}
Chapter 16, National Language Support.
The fnmatch subroutine,regcomp subroutine,regerror subroutine,regexec subroutine, regfree subroutine.