Coder Social home page Coder Social logo

regex's Introduction

regex

a pure C implementation of simple regular express engine, using DFA engine. the former is just for yes-or-no matching.

that's all begin from 正则表达式实现

Just for fun. ;)

pattern

pattern describe
$ matches the end of the string
ab matches a followed by b
matches a or b (ordered)
a* matches at least 0 repetitions of a
a? matches 0 or 1 repetitions of a
a+ matches 1 or more repetitions of a
(...) matches whatever regex is inside the parentheses, as a single element
[a-b] matches any characters between a and b (range)
. matches any characters, equivalent to the set [0-0xff]
\d matches any decimal digit, equivalent to the set [0-9]
\ escapes special characters

the following escaping sequences are supported:

escapes describe
\t tab
\n newline
\r return
\s space
\\ \
\( (
\) )
\[ [
\] ]
\- -
\. .
\+ +
\$ $

^ (matches the start of the string) is enabled by default.

todo list

  1. subset DFA
  2. backreference
  3. capture

api

// create/free a regex environment.
struct reg_env* reg_open_env();
void reg_close_env(struct reg_env* env);

// create/free a regex pattern.
struct reg_pattern* reg_new_pattern(struct reg_env* env, const char* rule);
void reg_free_pattern(struct reg_pattern* pattern);

// match pattern, return a boolean value.
int reg_match(struct reg_pattern* pattern, const char* source, int len);

see src/regex.h headfile for detail.

example

  // create env
  struct reg_env* env = reg_open_env();
  
  // create pattern
  struct reg_pattern* pattern = reg_new_pattern(env, rule);

  // match pattern
  int success = reg_match(pattern, source, strlen(source));

  // free env
  reg_close_env(env);

see test/test_api.c testcase for detail.

regex's People

Contributors

lvzixun avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.