Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <regex.h>
  5.  
  6.  
  7. int match (const char *string, char *pattern) {
  8.     int status;
  9.     regex_t re;
  10.  
  11.     // Compile the pattern for extended REGEX with only Y/N feedback (validates it too)
  12.     if (regcomp(&re, pattern, REG_EXTENDED | REG_NOSUB) != 0) {
  13.         return -1;
  14.     }
  15.  
  16.     // See if pattern validates input
  17.     status = regexec(&re, string, (size_t) 0, NULL, 0);
  18.  
  19.     // Free allocated memory
  20.     regfree(&re);
  21.  
  22.     // Output result on standard-out
  23.     if (status == 0) {
  24.         fprintf(stdout, "Match!\n");
  25.     } else {
  26.         fprintf(stdout, "No match!\n");
  27.     }
  28.  
  29.     return 0;
  30. }
  31.  
  32. int main (void) {
  33.  
  34.     const char string[3] = {0xFF, 0xFF, 0x0};
  35.     char *pattern = "^\\xFF\\xFF$";
  36.     printf("Pattern = %s\n", pattern);
  37.  
  38.     if (match(string, pattern) != 0) {
  39.         fprintf(stderr, "Err: Something went wrong in match!\n");
  40.         return EXIT_FAILURE;
  41.     }
  42.    
  43.     return EXIT_SUCCESS;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement