package pcre
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=671142f40b6d86171cbc067253faadf903019161d57488bd0fb6c5456c2cbd1a
sha512=be60f13ddb6bbfe20e30ca5d92434d85e1d1371479e1e2c725588af83fcc9366ed0435021b6a800c20336ac521f2134c767420136438684656a44ac1f9924be4
Description
pcre-ocaml offers library functions for string pattern matching and substitution, similar to the functionality offered by the Perl language.
Published: 24 Jul 2021
README
PCRE-OCaml - Perl Compatibility Regular Expressions for OCaml
This OCaml-library interfaces the C-library PCRE (Perl-compatibility Regular Expressions). It can be used for string matching with "PERL"-style regular expressions.
Features
PCRE-OCaml offers the following functionality for operating on strings:
Searching for patterns
Extracting subpatterns
Splitting strings according to patterns
Pattern substitution
Other reasons to use PCRE-OCaml:
The PCRE-library by Philip Hazel has been under development for many years and is fairly advanced and stable. It implements just about all of the functionality that can be found in PERL regular expressions. The higher-level functions written in OCaml (split, replace, etc.), too, are compatible with the corresponding PERL-functions to the extent that OCaml allows. Most people find the syntax of PERL-style regular expressions more straightforward and powerful than the Emacs-style regular expressions used in the
Str
-module in the standard OCaml distribution.PCRE-OCaml is reentrant and thus thread-safe, which is not the case for the
Str
-module in the OCaml standard library. Using reentrant libraries also means more convenience for programmers. They do not have to reason about states in which the library might be in.The high-level functions for replacement and substitution, which are all implemented in OCaml, are much faster than the ones in the
Str
-module. In fact, when compiled to native code, they even seem to be significantly faster than those found in PERL (PERL is written in C).You can rely on the data returned being unique. In other terms: if the result of a function is a string, you can safely use destructive updates on it without having to fear side effects.
The interface to the library makes use of labels and default arguments to give you a high degree of programming comfort.
Usage
Please consult the API for details.
A general concept the user may need to understand is that most functions allow for two different kinds of flags:
"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 = Pcre.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.
"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 = Pcre.cflags [`ANCHORED; `CASELESS] in for i = 1 to 1000 do let rex = Pcre.regexp ~iflags "some pattern constructed at runtime" in (* ... *) done
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 = Pcre.split ~pat:"[ \t]+" "foo bar" in (* ... *) done
Better:
let rex = Pcre.regexp "[ \t]+" in for i = 1 to 1000 do let chunks = Pcre.split ~rex "foo bar" in (* ... *) done
The provided functions use optional arguments with intuitive defaults. For example, the Pcre.split
-function will assume whitespace as pattern. The examples
-directory contains a few example applications demonstrating the functionality of PCRE-OCaml.
Restartable (partial) pattern matching
PCRE includes an "alternative" DFA match function that allows one to restart a partial match with additional input. This is exposed by pcre-ocaml
via the pcre_dfa_exec
function. While this cannot be used for "higher-level" operations like extracting submatches or splitting subject strings, it can be very useful in certain streaming and search use cases.
This utop
interaction demonstrates the basic workflow of a partial match that is then restarted multiple times before completing successfully:
utop # open Pcre;;
utop # let rex = regexp "12+3";;
val rex : regexp = <abstr>
utop # let workspace = Array.make 40 0;;
val workspace : int array =
[|0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0;
0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0|]
utop # pcre_dfa_exec ~rex ~flags:[`PARTIAL] ~workspace "12222";;
Exception: Pcre.Error Partial.
utop # pcre_dfa_exec ~rex ~flags:[`PARTIAL; `DFA_RESTART] ~workspace "2222222";;
Exception: Pcre.Error Partial.
utop # pcre_dfa_exec ~rex ~flags:[`PARTIAL; `DFA_RESTART] ~workspace "2222222";;
Exception: Pcre.Error Partial.
utop # pcre_dfa_exec ~rex ~flags:[`PARTIAL; `DFA_RESTART] ~workspace "223xxxx";;
- : int array = [|0; 3; 0|]
Please refer to the documentation of pcre_dfa_exec
and check out the dfa_restart
example for more info.
Contact Information and Contributing
Please submit bugs reports, feature requests, contributions and similar to the GitHub issue tracker.
Up-to-date information is available at: https://mmottl.github.io/pcre-ocaml
Dependencies (4)
-
conf-libpcre
build
- dune-configurator
-
ocaml
>= "4.12"
-
dune
>= "2.7"
Dev Dependencies (1)
-
odoc
with-doc
Used by (81)
- aifad
- annexlib
- anthill
-
aws
= "0.0.3"
- bap-veri
-
benchmark
< "1.3" | = "1.5"
- bigbro
-
biocaml
< "0.4.0"
- camelsnakekebab
-
camlp5
>= "8.00~alpha04" & < "8.02.01"
-
ccss
< "1.6"
- cduce
- chamo
-
coccinelle
>= "1.0.8"
- comby
- comby-kernel
-
commons
>= "1.8.0"
-
core_extended
< "109.45.00"
- cppffigen
- devkit
- dirsp-ps2ocaml
-
dotenv
>= "0.0.3"
-
duppy
< "0.9.4"
- expect
- genet
-
grib
< "0.11.0"
- hdf
- hydro
- janestreet_csv
-
jenga
< "113.24.00"
-
jingoo
< "1.2.21"
- lambdoc
-
lastfm
< "0.3.4"
-
ldap
< "2.5.1"
- line-up-words
-
liquidsoap
< "2.2.0"
- liquidsoap-core
- matita
-
mlt_parser
= "v0.11.0"
-
mparser
< "1.1"
- mparser-pcre
- netamqp
-
oasis
< "0.3.0"
- oasis2debian
- ocaml-http
- ocaml-inifiles
- ocaml_db_model
- ocaml_pgsql_model
-
ocamldap
< "transition"
- ocamldbi
-
ocamlnet
< "3.6.0"
-
ocsigenserver
< "5.1.0"
-
opium
< "0.6.0"
- orakuda
-
pa_ppx
< "0.14"
- pa_ppx_ag
-
pa_ppx_hashcons
< "0.11"
-
pa_ppx_migrate
< "0.11"
-
pa_ppx_q_ast
< "0.11"
- pa_ppx_regexp
-
pa_ppx_unique
< "0.11"
-
patdiff
>= "109.45.00" & < "v0.16.1"
-
pgocaml
< "2.3"
- plasma
-
plotkicadsch
< "0.2.0"
- ppx_orakuda
-
ppx_test
< "1.1.0"
-
rdf
>= "0.6.0"
- rdf_impls
- rotor
-
server-reason-react
< "0.2.0"
- shcaml
-
sihl
< "0.1.5"
-
spectrum
>= "0.4.0"
- squirrel
- stk
-
stog
= "0.5"
-
textmate-language
< "0.2.0"
-
tyxml
< "3.0.0"
- wdialog
-
webdav
< "1.1.5"
Conflicts
None