SLRE - C语言超轻量级正则库

SLRE is an ANSI C library that implements a tiny subset of Perl regular expressions. It is primarily targeted for developers who want to parse configuation files, where speed is unimportant. It is in single .c file, easily modifiable for custom needs. For example, if one wants to introduce a new metacharacter, 'i', that means 'IP address', it is easy to do so.



SLRE是一个ANSI C库。实现了一个精简版本的Perl正则。主要目标用于解析配置文件,性能不是很好。只有一个.c源文件,便于修改。比如想引入一个新的元字符'i'来表示IP地址,是很容易做到的。


Features

  • Crossplatform - pure ANSI C
  • Very simple API
  • Light: about 5kB of code when compiled
  • Uses no dynamic memory allocation
  • Thread safe
  • 跨平台 - 标准C
  • 简单接口
  • 轻量级
  • 不使用动态内存分配
  • 线程安全


Supported RE Syntax

      ^               Match beginning of a buffer
      $               Match end of a buffer
      ()              Grouping and substring capturing
      [...]           Match any character from set
      [^...]          Match any character but ones from set
      s              Match whitespace
      S              Match non-whitespace
      d              Match decimal digit
      
              Match carriage return
      
              Match newline
      +               Match one or more times (greedy)
      +?              Match one or more times (non-greedy)
      *               Match zero or more times (greedy)
      *?              Match zero or more times (non-greedy)
      ?               Match zero or once
      Ý            Match byte with hex value 0xDD
      meta           Match one of the meta character: ^$().[*+?

int slre_match(const char *regexp, const char *buf, int buf_len,
               struct slre_cap *caps, int num_caps, int flags);

slre_match() matches the string buffer buf in length buf_len against the regular expression regexp, which should conform the syntax outlined above. If the regular expression regexp contains brackets, slre_match() can capture the respective substrings into the array of struct slre_cap structures:

/* Stores matched fragment for the expression inside brackets */
struct slre_cap {
  const char *ptr;  /* Points to the matched fragment */
  int len;          /* Length of the matched fragment */
};

N-th member of the caps array will contain fragment that corresponds to the N-th opening bracket in the regex, N is zero-based. slre_match() returns the number of bytes scanned from the beginning of the string. If the return value is greater or equal to 0, there is a match. If the return value is less then 0, there is no match. Negative return codes are as follows:

#define SLRE_NO_MATCH               -1
#define SLRE_UNEXPECTED_QUANTIFIER  -2
#define SLRE_UNBALANCED_BRACKETS    -3
#define SLRE_INTERNAL_ERROR         -4
#define SLRE_INVALID_CHARACTER_SET  -5
#define SLRE_INVALID_METACHARACTER  -6
#define SLRE_CAPS_ARRAY_TOO_SMALL   -7
#define SLRE_TOO_MANY_BRANCHES      -8
#define SLRE_TOO_MANY_BRACKETS      -9

Valid flags are:

  • SLRE_IGNORE_CASE: do case-insensitive match

猜你喜欢

转载自blog.csdn.net/qq_41551359/article/details/81040117