re
Re is a regular expression library for OCaml.
Contact
This library has been written by Jerome Vouillon
(Jerome.Vouillon@pps.univ-paris-diderot.fr).
It can be downloaded from https://github.com/ocaml/ocaml-re
Bug reports, suggestions and contributions are welcome.
Features
The following styles of regular expressions are supported:
Perl-style regular expressions (module
Re.Perl
);Posix extended regular expressions (module
Re_posix
);Emacs-style regular expressions (module
Re.Emacs
);Shell-style file globbing (module
Re_glob
).
It is also possible to build regular expressions by combining simpler
regular expressions (module Re
).
The most notable missing features are back-references and
look-ahead/look-behind assertions.
There is also a subset of the PCRE interface available in theRe.pcre
library. This makes it easier to port code from that
library to Re without any changes beyond replacing the pcre
findlib package with re.pcre
.
Performances
The matches are performed by lazily building a DFA (deterministic
finite automaton) from the regular expression. As a consequence,
matching takes linear time in the length of the matched string.
The compilation of patterns is slower than with libraries using
back-tracking, such as PCRE. But, once a large enough part of the
DFA is built, matching is extremely fast.
Of course, for some combinations of regular expression and string, the
part of the DFA that needs to be build is so large that this point is
never reached, and matching will be slow. This is not expected to
happen often in practice, and actually a lot of expressions that
behaves badly with a backtracking implementation are very efficient
with this implementation.
The library is at the moment entirely written in OCaml. As a
consequence, regular expression matching is much slower when the
library is compiled to bytecode than when it is compiled to native
code.
Here are some timing results (Pentium III 500Mhz):
Scanning a 1Mb string containing only
a
s, except for the last
character which is ab
, searching for the patternaa?b
(repeated 100 times):RE: 2.6s
PCRE: 68s
Regular expression example from http://www.bagley.org/~doug/shootout/ [1]
RE: 0.43s
PCRE: 3.68s
[1] this page is no longer up but is available via the Internet Archive
http://web.archive.org/web/20010429190941/http://www.bagley.org/~doug/shootout/bench/regexmatch/The large regular expression (about 2000 characters long) that
Unison uses with my preference file to decide whether a file should
be ignored or not. This expression is matched against a filename
about 20000 times.RE: 0.31s
PCRE: 3.7s
However, RE is only faster than PCRE when there are more than about
300 filenames.
< "0.4.3" | >= "1.0.0"
>= "1.3.0"
>= "1.3.0"
>= "1.3.0"
= "2.2.1"
>= "2.3.3"
= "2.2.1"
>= "2.2.1" & < "20151001.01"
>= "2.4.1"
< "2.3.3"
< "1.0.0"
< "1.4.0" | >= "1.6.0"
>= "2.2.0"
>= "0.4.0"
= "0.5.0"
>= "0.2"
>= "1.7.0"
>= "1.6"
>= "v0.10.0" & < "v0.12.0"
< "2.1.3"
>= "v0.9.0" & < "v0.12.0"
>= "v0.10.0" & < "v0.12.0"
< "1.2.2"
>= "0.4.0"
< "0.1.2"
>= "0.8" & < "1.0.0"
< "4.0.0"
< "0.2.1"
>= "0.5.1"
>= "0.2.0"
< "v0.12.0"
!= "0.12.0"
< "1.3.0" | >= "1.9.0" & < "1.10.0"
>= "0.11.0"
>= "0.0.3"
< "0.3.3"
>= "0.1.9"
< "1.3.2" | = "1.4.0"
!= "0.8.1" & < "0.10.0" | >= "1.0.0" & < "1.2.0"
>= "1.2.21"
>= "3.10.0"
< "0.2.0"
>= "0.13.0"
>= "0.3"
>= "0.10.0" & < "2.4.0"
< "0.3.2"
< "1.0.0"
< "2.0.0"
>= "0.5.0"
>= "1.7.0"
>= "0.11.0"
>= "0.19.0" & < "0.21.0"
>= "1.0.2" & < "1.2"
>= "1.7.0"
>= "2.0.0" & < "2.1.0"
>= "0.9.5" & < "1.1.0"
>= "2.0.0~rc2" & < "2.1.0~beta2"
< "2.1.0~beta2"
>= "2.0.8" & < "2.1.0~beta2"
>= "2.0.8" & < "2.1.0~beta2"
>= "1.3.0"
>= "0.13.1"
< "3.0.1"
>= "2.3"
>= "0.3"
< "v0.12.0"
>= "0.1.0"
>= "3.0.0"
>= "0.3.2"
>= "1.1.0" & < "1.7.0"
>= "0.2"
< "1.7.4"
>= "0.2.1"
< "1.0.0"
>= "20210823"
< "v0.12.0"
>= "1.3.1"
< "3.2.0"
>= "20210823"
>= "0.7.1" & < "0.9.0"
>= "0.2.0"
< "7.4"
< "8.0" | >= "13.0"
= "10.2" | >= "13.0"
>= "7.4"
>= "2.0.0"
>= "3.5.0"
< "0.4"
>= "4.0.0"
< "2.2.1"
>= "0.12.0"
>= "0.9.6"
>= "0.15.0" & < "0.17.0"