Regular Expressions in OCaml

旧巷老猫 提交于 2019-12-23 06:48:17

问题


I want to use regexps in OCaml and it seems that Str module provides these functionalities.

So I tried with a simple program:

open Str
let regx = regexp "."

but it gives me the following error

File "lol.ml", line 1, characters 0-1: Error: Error while linking lol.cmo: Reference to undefined global `Str'

As if module is not present but if I remove open Str it says that regexp is an unbound value.

I don't get what kind of issue it is, Str should be a standard module (according to http://caml.inria.fr/pub/docs/old-311/libref/Str.html) so I'm clueless.. the only think I thought is that signature (mli) is present but implementation (ml) is not.

I'm running Objective Caml version 3.11.0 according to ocaml tool.

Can anyone help me figuring this out? Thanks in advance


回答1:


From the manual:

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



回答2:


Or you can put

#load "str.cma";;

if you are doing it in the interpreter




回答3:


As an alternative to the Str module there's also Re2.

  1. Install it using opam install re2
  2. Use the module in your_file.ml like this:

    open Re2.Std
    open Re2.Infix
    let change input_text = Re2.rewrite_exn ~/"change this" "to that" input_text
    let () = printf "%s" (change "change this")
    
  3. Compile with ocamlbuild -use-ocamlfind -package re2 -package core -tag thread your_file.byte



来源:https://stackoverflow.com/questions/3221067/regular-expressions-in-ocaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!