GithubHelp home page GithubHelp logo

skyformat99 / dre2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jondurbin/dre2

0.0 1.0 0.0 29.9 MB

DRE2 - A fast, lightweight regex engine in c.

C 98.37% C++ 0.52% Ruby 0.10% Shell 0.90% Perl 0.12%

dre2's Introduction

#dre2 - An extremely fast, lightweight regex engine written in c.

DRE2 options:

Thread-safe - MUST be used in any multi-threaded application.

  re = dre2_parse( regex_string, DRE2_THREAD_SAFE );

Full match - Entire string must match the regex:

  re = dre2_parse( regex_string, DRE2_FULL_MATCH );

Case-insensitive regex:

  re = dre2_parse( regex_string, DRE2_NO_CASE );

Greedy mode:

  re = dre2_parse( regex_string, DRE2_GREEDY );

Submatch mode - Needed if you plan on using the submatches for anything.

  re = dre2_parse( regex_string, DRE2_SUBMATCH );

Combining options:

Entire string must match + ignore case.

  re = dre2_parse( regex_string, DRE2_FULL_MATCH | DRE2_NO_CASE );

Submatch mode + ignore case + thread safe:

  re = dre2_parse( regex_string, DRE2_SUBMATCH | DRE2_NO_CASE | DRE2_THREAD_SAFE );

Functions:

Generating the dre2 object from a regular expresion string.

  struct dre2 *re;
  re = dre2_parse( regex_string, 0 );
  if ( re == NULL )
  {
    // Parse failure.
    printf( "Failed to parse!\n" );
  }

Matching an input string with the dre2 object:

  struct dre2_match_value result;
  result = dre2_match( re, input_string );
  if ( result.matched )
  {
    // Match successful.
    printf( "Match.\n" );
  }

Extracting the matching substring.

    unsigned char *match;
    match = ( unsigned char * )malloc( strlen( buf ) * sizeof( unsigned char ) );
    result = dre2_match( re, buf );
    if ( result.matched )
    {
      dre2_matched_substring( buf, &result, &match );
      printf( "%s\n", match );
    }

Extracting the submatches.

  // Initialize submatch string array.
  submatches = ( unsigned char ** )calloc( re->original->group_count, sizeof( unsigned char * ) );
  for ( i = 1; i < re->original->group_count; i++ )
    submatches[i - 1] = ( unsigned char * )calloc( 0x10000, sizeof( unsigned char ) );

  // Check if input string matches the regex.
  result = dre2_match( re, input );
  if ( result.matched )
  {
    // First, get the matched substring.
    dre2_matched_substring( input, &result, &match );
    printf( "Match: %s\n", match );

    // We only have submatch info if there is more than 1 group.
    if ( re->original->group_count > 1 )
    {
      // Run the backtracking match to set submatched strings.
      dre2_backtrack_match( re, match, &submatches );
      for ( i = 1; i < re->original->group_count; i++ )
        printf( "Submatch %d: '%s'\n", i - 1, submatches[i - 1] );
    }
  }

Cleanup - Frees memory used by the dre2 object:

  cleanup_dre2( re );

For full examples, see dre2.c and submatch.c.

dre2's People

Contributors

jondurbin avatar

Watchers

 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.