Last active
July 12, 2017 08:52
-
-
Save jweyrich/9803969 to your computer and use it in GitHub Desktop.
PCRE test related to https://github.com/merces/pev/issues/60
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Compile and run: | |
// LDFLAGS="-lpcre" make pcre_ucp_test && ./pcre_ucp_test | |
// | |
#include <stdbool.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pcre.h> | |
#define SIZEOF_ARRAY(array) sizeof(array) / sizeof(*array) | |
bool regex_match(const char *pattern, const char *value, bool use_unicode) { | |
const char *err; | |
int errofs; | |
int ovector[1000]; | |
pcre *re = pcre_compile(pattern, use_unicode ? PCRE_UCP : 0, &err, &errofs, NULL); | |
if (re == NULL) { | |
fprintf(stderr, "%s regex compilation failed\n", use_unicode ? "PCRE_UCP" : "ASCII"); | |
exit(EXIT_FAILURE); | |
} | |
const int rc = pcre_exec(re, NULL, value, sizeof(value), 0, 0, ovector, SIZEOF_ARRAY(ovector)); | |
pcre_free(re); | |
return rc > 0; | |
} | |
int main() { | |
const char pattern[] = "^[a-zA-Z]{3,}://.*$"; | |
const char value[] = "http://www.example.com/foo/bar"; | |
bool matches; | |
matches = regex_match(pattern, value, false); | |
printf("%s matches: %s\n", "ASCII", matches ? "yes" : "nope"); | |
matches = regex_match(pattern, value, true); | |
printf("%s matches: %s\n", "PCRE_UCP", matches ? "yes" : "nope"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mac output:
Linux output: