如何在OCaml中与模块Str进行完全(急切)匹配

时间:2014-01-28 22:50:43

标签: regex ocaml

我试图为这个标准问题找到合适的函数,例如在Tcl shell中:

str@suse131-intel:~> tclshi
% regexp -inline {[0-9]+} "I am trying this for the 1001. time!!"
1001
%

我已经尝试了很多,我可以找到比赛的开始和结束,但这不可能!我只得到最小的比赛,而不是热切的比赛。我应该手动挑选比赛,那么说呢? Ocaml互动:

# let s1 = "this is the 1001. time I am trying this.";;
val s1 : string = "this is the 1001. time I am trying this."
# search_forward (regexp "[0-9]+") s1 0;;
- : int = 12
# group_beginning 0;;
- : int = 12
# group_end 0;;
- : int = 16

子问题:所有这些引用最后一个匹配的函数(“返回与最后一次调用匹配的s的子字符串,如文档所说”)就像matches_string一样,看起来不是FP,但恰好是相反。我这个可重入和任务切换保存代码?

编辑:在昨天的会话中,我只收到了一个号码。现在我想将这个奇怪的行为复制到这个问题中,但它工作得很好。问题可能是不同的定义的混合。

完整示例:

# #require "str";;
/usr/lib64/ocaml/str.cma: loaded
# open Str;;
# let s1 = "this is the 1001. time I am trying this.";;
val s1 : string = "this is the 1001. time I am trying this."
# search_forward (regexp "[0-9]+") s1 0;;
- : int = 12
# matched_string s1;;
- : string = "1001"

1 个答案:

答案 0 :(得分:2)

OCaml代码似乎与tcl代码完全匹配相同的子字符串,因此我不确定您正在寻找什么额外的热情。

你是对的,这个字符串匹配的接口不起作用。它取决于隐藏状态,很可能不是线程安全的。 OCaml Batteries Included有Str.search,它稍微好一些,它表明接口的非FP部分应该被认为是过时的。