Previous Contents Next
Chapter 22 The str library: regular expressions and string processing

The str library provides high-level string processing functions, some based on regular expressions. It is intended to support the kind of file processing that is usually performed with scripting languages such as awk, perl or sed.

Programs that use the str library must be linked as follows:
        ocamlc other options str.cma other files
        ocamlopt other options str.cmxa other files
For interactive use of the str library, do:
        ocamlmktop -o mytop str.cma
        ./mytop
or (if dynamic linking of C libraries is supported on your platform), start ocaml and type #load "str.cma";;.

22.1 Module Str: regular expressions and high-level string processing


Regular expressions
type regexp
The type of compiled regular expressions.
val regexp: string -> regexp
Compile a regular expression. The syntax for regular expressions is the same as in Gnu Emacs. The special characters are $^.*+?[]. The following constructs are recognized:
. matches any character except newline
* (postfix) matches the previous expression zero, one or several times
+ (postfix) matches the previous expression one or several times
? (postfix) matches the previous expression once or not at all
[..] character set; ranges are denoted with -, as in [a-z]; an initial ^, as in [^0-9], complements the set
^ matches at beginning of line
$ matches at end of line
\| (infix) alternative between two expressions
\(..\) grouping and naming of the enclosed expression
\1 the text matched by the first \(...\) expression (\2 for the second expression, etc)
\b matches word boundaries
\ quotes special characters.
val regexp_case_fold: string -> regexp
Same as regexp, but the compiled expression will match text in a case-insensitive way: uppercase and lowercase letters will be considered equivalent.
val quote: string -> string
Str.quote s returns a regexp string that matches exactly s and nothing else.