Onigmo Regular Expressions Library

repository·master·Indexed 20 days ago

https://github.com/k-takata/onigmo

A regular expressions library forked from Oniguruma that provides advanced regex support similar to Perl 5.10+, including features like \K, \R, and conditional expressions. It serves as the default regex engine for Ruby 2.0+. The library supports C/C++ integration via onigmo.h and oniguruma.h, and provides build instructions for Unix, Cygwin, and Windows (MinGW, Visual C++).

Tokens
1.5K
Snippets
8
Records
12
Agent score
68%

What's inside Onigmo

  1. Install Onigmo on Windows (MinGW)

    master

    For Windows users using MinGW, use the provided Makefile. This will create directories like build_x86-64 or build_i686.

    Build command:

    mingw32-make -f win32/Makefile.mingw

    Generated files:

    • libonigmo.a: Static link library
    • libonigmo.dll.a: Import library for dynamic linking
    • onigmo.dll: Dynamic link library

    Testing: Execute mingw32-make -f win32/Makefile.mingw test. Python (matching the bitness of the Onigmo build) is required.

    Note for MSYS2 users: You can also use the standard ./configure and make workflow. In this case, the DLL name will include the API version (e.g., libonigmo-6.dll).

  2. Use the Onigmo API in C/C++

    master

    To use Onigmo in your application, include the onigmo.h header file.

    Handling Name Collisions: If you encounter naming conflicts with existing types, you can disable them by defining specific macros before including the header:

    • To disable UChar (as unsigned char): Define ONIG_ESCAPE_UCHAR_COLLISION.
    • To disable regex_t: Define ONIG_ESCAPE_REGEX_T_COLLISION.

    Compiling and Linking (Unix/Cygwin example): If Onigmo is installed in /usr/local, link using -lonigmo:

    cc sample.c -L/usr/local/lib -lonigmo

    Static Linking on Win32: If you want to use the static link library (onigmo_s.lib) on Windows, add the following option to your C compiler: -DONIG_EXTERN=extern

  3. What is Onigmo?

    master

    Onigmo (Oniguruma-mod) is a regular expressions library forked from Oniguruma. It is designed to support advanced Perl 5.10+ expressions and is used as the default regular expression library for Ruby 2.0 or later.

    Key features include support for:

    • Advanced regex tokens: \K, \R, \X, (?(cond)yes|no), (?adlu), \g{name}, \g{n}, (?&name), (?n), (?R), (?0), (?P<name>...), (?P=name), and (?P>name).
    • Specific API extensions: onig_search_gpos (for Perl-compatible \G).
    • Python syntax support.
    • Additional encodings like CP932, CP1250, CP1251, CP1252, CP1253, and CP1254.
  4. New regular expression features in Onigmo

    master

    Onigmo supports several advanced regular expression syntaxes:

    • Control/Lookaround: \K, \R, \X (Unicode extended grapheme cluster), (?(cond)yes|no)
    • Grammar-dependent: (?adlu), \g{name}, \g{n}, (?&name), (?n), (?R), (?0)
    • Named Groups: (?P<name>...), (?P=name), (?P>name)
  5. Compile and link Onigmo on Unix/Cygwin

    master

    When compiling and linking on Unix/Cygwin (assuming a prefix of /usr/local), use the following command:

    cc sample.c -L/usr/local/lib -lonig

    Note: Onigmo uses GNU libtool. If your platform supports shared libraries, you can use them. For static linking on Win32 with onig_s.lib, add -DONIG_EXTERN=extern to your compilation arguments.

  6. Install Onigmo on Unix and Cygwin

    master

    To install Onigmo on Unix-like systems or Cygwin, use the standard autotools build process:

    1. Run ./autogen.sh if the configure script is not present.
    2. Run ./configure to prepare the build.
    3. Run make to compile.
    4. Run make install to install the library to the system.

    To uninstall, run make uninstall.

    To run tests (ASCII/EUC-JP), run make atest.

    You can check your configuration using onig-config:

    • --cflags
    • --libs
    • --prefix
    • --exec-prefix
    ./autogen.sh
    ./configure
    make
    make install
  7. Compile and link Onigmo in C/C++

    master

    To use Onigmo in your program, include oniguruma.h in your source code.

    Unix/Cygwin Linking Example

    If your installation prefix is /usr/local, compile your program using:

    cc sample.c -L/usr/local/lib -lonig

    Avoiding Name Collisions

    If you need to avoid collisions with existing definitions in your environment, you can define the following macros before including the header:

    • ONIG_ESCAPE_UCHAR_COLLISION: Disables the UChar type definition.
    • ONIG_ESCAPE_REGEX_T_COLLISION: Disables the regex_t type definition.
  8. Install Onigmo on Windows (Visual C++)

    master

    To build Onigmo on Windows using Visual C++ (nmake):

    1. Copy win32\Makefile to the root directory as Makefile.
    2. Copy win32\config.h to the root directory as config.h.
    3. Run nmake.

    This produces:

    • onig_s.lib: Static link library.
    • onig.dll: Dynamic link library.

    To test (ASCII/Shift_JIS):

    1. Copy win32\testc.c to testc.c.
    2. Run nmake ctest.

    Note: If you want to use the static link library (onig_s.lib), you must add the -DONIG_EXTERN=extern option to your C compiler.

    copy win32\Makefile Makefile
    copy win32\config.h config.h
    nmake
  9. Explore Onigmo Sample Programs

    master

    The repository includes several sample programs to demonstrate different aspects of the Onigmo API:

    FileDescription
    sample/simple.cMinimum Onigmo API usage example
    sample/names.cNamed group callback example
    sample/encode.cEncoding examples
    sample/listcap.cCapture history example
    sample/posix.cPOSIX API sample
    sample/sql.cVariable meta characters example
    sample/syntax.cPerl, Java, and ASIS syntax test
    sample/crnl.cCRNL test
  10. Use the Oniguruma API in C/C++

    master

    To use the library in your program, include oniguruma.h.

    Handling Type Collisions

    If you need to avoid collisions with existing type definitions, you can use the following macros before including the header:

    • To disable the UChar type (which is unsigned char): Define ONIG_ESCAPE_UCHAR_COLLISION. This makes only OnigUChar available.
    • To disable the regex_t type: Define ONIG_ESCAPE_REGEX_T_COLLISION. This makes OnigRegexType and OnigRegex available instead.
    #define ONIG_ESCAPE_UCHAR_COLLISION
    #include "oniguruma.h"