I want some help in writing regular expressions in Ocaml, as I know how to write it in informal way but in Ocaml syntax I can not. For example I want to write "a* | (aba)* ".
Another question if I want the string to be matched against the regular expression to be matched as whole string not as substring what symbol I need to attach to the substring, i.e if I want only concrete strings accepted (like (" ", a , aa , aaa, aba, abaaba), but not ab or not abaa).
Hint I am using (Str.regexp)
This question would better be posted on the beginners' list - http://caml.inria.fr/resources/forums.en.html#id2267683
Regular Expressions can be done using the Standard Library with the Str module (as you've found) - see http://caml.inria.fr/pub/docs/manual-ocaml/libref/Str.html so your expression above (assuming you have loaded/linked str.cm[x]a) is Str.regexp "a*\\|\\(aba\\)*". The language of regexps is given in the docs for Str.regexp function. Remember to escape backslash characters as the regular expression is given in an OCaml string (so to escape a backslash in your regexp you have to write "\\\\").
> Another question if I want the string to be matched against the regular expression
> to be matched as whole string not as substring what symbol I need to attach to the
> substring, i.e if I want only concrete strings accepted (like (" ", a , aa , aaa,
> aba, abaaba), but not ab or not abaa).
Use ^ and $ at the beginning and end of your regexp to ensure that it matches the entire string only - "^\\(a*\\|\\(aba\\)*\\)$"
> Hint I am using (Str.regexp)
There are other libraries (e.g. pcre-ocaml) which provide different (I would say more powerful, rather than strictly better!) implementations.
Caml-list mailing list. Subscription management:
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
There is also syntax extension like mikmatch, that helps to write regexp
in a very meaningful syntax:
match str with
| RE bol "a"* | "ab"* eol ->
| _ ->
You can use pcre and str with mikmatch.
Sylvain Le Gall
I also had problems with Str (regexp descriptions being unreadable,
error-prone and hard to generate dynamically) and decided just to stop
I have a tiny module  made with clarity in mind. It is pure OCaml. It
defines operators like $$ to be used in regexp construction. This way
syntax of the expressions is checked at compile time. Also, it is
trivial to build them at run time.
The whole "engine" is contained in a relatively short function
HRegex.subwords_of_subexpressions, so I believe anybody can hack it
without much effort.
I haven't measured performance of this implementation. I expect it to be
slow when processing long strings. It's just OK for my needs so far.
Anyway, the important part is the module interface. It expresses my
point of view on this topic.
The code is available in a mercurial repository .
The exemple "a* | (aba)* " would become:
let rx = (!* !$ "a") +$ (!* !$ "aba")
If I understand correctly the original problem, the solution is:
match str with
| RE ("a"* | "aba"*) eos ->
(* matches always the beginning of the string,
eos enforces a match at the end of the string,
and the vertical bar has the lowest priority
and so parentheses are needed. *)
| _ ->
I would recommend the pcre variant mostly for one feature that is not
provided by str: lazy quantifiers, i.e. "repeat as little as possible
before trying to match what comes next".