Advertisement
cd62131

RegexMatch

Jul 19th, 2014
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.67 KB | None | 0 0
  1. #include <regex.h>
  2. #include <stdio.h>
  3. static int match(const char *string, const char *regex) {
  4.   regex_t preg;
  5.   regcomp(&preg, regex, REG_EXTENDED);
  6.   if (!regexec(&preg, string, 0, NULL, 0)) return 1;
  7.   return 0;
  8. }
  9. int main(void) {
  10.   int i;
  11.   const char * const greeting[] = {
  12.       "○○さん、こんにちは",
  13.       "こんにちわ、○○さん",
  14.       "○○さん、こんばんは",
  15.       "こんばんわ、○○さん"
  16.   };
  17.   const char hello[] = "こんにち(わ|は)";
  18.   for (i = 0; i < sizeof(greeting) / sizeof(char **); i++) {
  19.     if (match(greeting[i], hello))
  20.       printf("%s\nこんにちは\n", greeting[i]);
  21.   }
  22.   return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement