PCRE

该页面还没有翻译。

PCRE

类型正规表示式
授权协议BSD License
网站https://www.pcre.org/

Perl Compatible Regular Expressions(PCRE) is a regular expression Library capabilities in the Perl.

C语言的范例程式

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pcre.h>

int main()
{
  pcre *re;
  const char *errptr;
  int erroffset;
  int capture_count;
  int *matches;
  int n_matches;
  int offset = 0;

  const char *pattern = "a(b*)c";
  const char *source = "xiomfseabbBBbbcdegg";

  re = pcre_compile(pattern, PCRE_CASELESS, &errptr, &erroffset, NULL);
  if (re == NULL) {
    fprintf(stderr, "%s at %d\n", errptr, erroffset);
    exit(1);
  }

  pcre_fullinfo(re, NULL, PCRE_INFO_CAPTURECOUNT, &capture_count);
  matches = malloc(sizeof(int) * (capture_count + 1) * 3);

  n_matches = pcre_exec(re, NULL, source, strlen(source), offset, 0, matches, (capture_count + 1) * 3);
  if (n_matches >= 0) {
    int i;
    for (i = 0; i < n_matches; i++) {
      int begin = matches[i * 2];
      int end = matches[i * 2 + 1];
      printf("(%d) : %.*s\n", i, end - begin, &source[begin]);
    }
  } else {
    printf("pattern not found\n");
  }

  return 0;
}

UTF-8支持

Comfirm UTF-8 is supported or not

$ pcretest -C
PCRE version 7.5 2008-01-10
Compiled with
  UTF-8 support
  Unicode properties support
  Newline sequence is LF
  \R matches all Unicode newlines
  Internal link size = 2
  POSIX malloc threshold = 10
  Default match limit = 10000000
  Default recursion depth limit = 10000000
  Match recursion uses stack

Make UTF-8 enabled

$ ./configure --enable-utf8 --enable-unicode-properties
$ make
# make install