Skip to content

Instantly share code, notes, and snippets.

@jweyrich
Last active July 12, 2017 08:52
Show Gist options
  • Save jweyrich/9803969 to your computer and use it in GitHub Desktop.
Save jweyrich/9803969 to your computer and use it in GitHub Desktop.
//
// 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;
}
@akzhan
Copy link

akzhan commented Jul 12, 2017

Just found the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment