【问题标题】:How to get a complete (eager) match with module Str in OCaml如何在 OCaml 中获得与模块 Str 的完整(渴望)匹配
【发布时间】:2023-04-01 12:55:02
【问题描述】:

几个小时以来,我一直在尝试为这个标准问题找到正确的函数,例如在 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 的子字符串,如文档所述”),如matched_string 看起来不是很FP,但正是对面的。我这个可重入和任务切换保存代码吗?

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

完整示例:

# #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"

【问题讨论】:

  • 您可能想尝试新的ocaml-re 库。它更新了,可能 API 更好。
  • 我知道,有 extlib、电池、Jane Street 的东西和......你说出来,但在深入研究这些之前,我想确定我已经理解了内置的 ocaml 的可能性模块。
  • @AshishAgarwal str 兼容 api 是同样丑陋的非线程安全的。我认为它的任何其他前端也不完整。

标签: regex ocaml


【解决方案1】:

OCaml 代码似乎与 tcl 代码匹配完全相同的子字符串,所以我不确定你在寻找什么额外的渴望。

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

【讨论】:

  • 我得到了匹配的位置,但我没有得到匹配。所以我必须做一个 String.sub?
  • 是的,你需要自己拉出子串。毫无疑问,如果有一个更友好的界面就好了。
猜你喜欢
  • 1970-01-01
  • 2020-08-21
  • 2011-07-24
  • 2013-08-25
  • 2015-08-26
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多