OCaml 中的正则表达式

发布于 2024-09-09 06:46:26 字数 713 浏览 0 评论 0原文

我想在 OCaml 中使用regexps,并且 Str 模块似乎提供了这些功能。

所以我尝试了一个简单的程序:

open Str
let regx = regexp "."

但它给了我以下错误

文件“lol.ml”,第 1 行,字符 0-1: 错误:链接 lol.cmo 时出错: 引用未定义的全局“Str”

就好像模块不存在一样,但如果我删除 open Str ,它会说 regexp 是一个未绑定的值。

我不明白它是什么类型的问题, Str 应该是一个标准模块(根据 http://caml.inria.fr/pub/docs/old-311/libref/Str.html)所以我一无所知..唯一我认为签名(mli)存在,但实现(ml)不存在。

我正在根据 ocaml 工具运行 Objective Caml version 3.11.0

谁能帮我解决这个问题吗? 提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌︼了一个春 2024-09-16 06:46:26

来自手册

使用 str 库的程序必须按如下方式链接:

ocamlc 其他选项 str.cma 其他文件
ocamlopt 其他选项 str.cmxa 其他文件

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
旧城空念 2024-09-16 06:46:26

则可以输入

#load "str.cma";;

或者,如果您在解释器中执行此操作,

Or you can put

#load "str.cma";;

if you are doing it in the interpreter

纸伞微斜 2024-09-16 06:46:26

作为 Str 模块的替代方案,还有 Re2

  1. 使用 opam install re2 安装它
  2. 使用 your_file.ml 中的模块,如下所示:

    打开Re2.Std
    打开Re2.Infix
    让改变input_text = Re2.rewrite_exn〜/“更改这个”“到那个”input_text
    let () = printf "%s" (更改“更改此”)
    
  3. 使用 ocamlbuild -use-ocamlfind -package re2 -package core -tag thread your_file.byte 进行编译

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文