PCRE-OCaml - Perl Compatibility Regular Expressions for OCaml

What is PCRE-OCaml?

This OCaml-library interfaces the C-library PCRE (Perl-compatibility Regular Expressions). It can be used for matching regular expressions which are written in "PERL"-style.


PCRE-OCaml offers the following functionality for operating on strings:

Other reasons to use PCRE-OCaml:


The API-documentation which is built and installed with PCRE-OCaml explains all functions, most of which are fairly straightforward to understand, in detail. It can also be found online.

A general concept the user may need to understand is that most functions allow for two different kinds of flags:

  1. "Convenience"-flags that make for readable and concise code, but which need to be translated to an internal representation on each call. Example:

     let rex = regexp ~flags:[`ANCHORED; `CASELESS] "some pattern" in
     (* ... *)

    This makes it easy to pass flags on the fly. They will be translated to the internal format automatically. However, if this happens to be in a loop, this translation will occur on each iteration. If you really need to save as much performance as possible, you should use the next approach.

  2. "Internal" flags that need to be defined and translated from "convenience"-flags before function calls, but which allow for optimum performance in loops. Example:

     let iflags = cflags [`ANCHORED; `CASELESS] in
     for i = 1 to 1000 do
       let rex = regexp ~iflags "some pattern constructed at runtime" in
       (* ... *)

    Factoring out the translation of flags for regular expressions may save some cycles, but don't expect too much. You can save more CPU time when lifting the creation of regular expressions out of loops. Example for what not to do:

      for i = 1 to 1000 do
        let chunks = split ~pat:"[ \t]+" "foo bar" in
        (* ... *)


      let rex = regexp "[ \t]+" in
      for i = 1 to 1000 do
        let chunks = split ~rex "foo bar" in
        (* ... *)

The provided functions use optional arguments with intuitive defaults. For example, the split-function will assume whitespace as pattern.

Take a look at the interface file pcre.mli to see which ways exists to pass parameters and to learn about the defaults. The examples-directory contains a few example applications demonstrating the functionality of PCRE-OCaml.

Contact Information and Contributing

In the case of bugs, feature requests, contributions and similar, you can contact me here: markus.mottl@gmail.com

Up-to-date information should be available at: http://mmottl.github.io/pcre-ocaml


Markus Mottl on July 10, 2012