【问题标题】:(ReasonML) Regex \b not matching(ReasonML) 正则表达式 \b 不匹配
【发布时间】:2017-07-13 18:35:04
【问题描述】:

我正在尝试 Reason,我正在尝试使用正则表达式,但没有任何匹配项。 AFAIK Reason 没有任何特定的正则表达式相关的东西,所以我只是离开 OCaml 文档。

我的字符串看起来像:

let name = "const {foo} = bar";

我使用的正则表达式是

let re = Str.regexp_string "\\bfoo\\b"
try {
  Str.search_forward re name 0;
  /* do something */
} {
 | Not_found => acc
}

但我没有得到任何匹配。有人可以帮忙吗?

编辑:作为参考,OCaml 的正则表达式文档可以在https://caml.inria.fr/pub/docs/manual-ocaml/libref/Str.html 找到。我使用 http://pleac.sourceforge.net/pleac_ocaml/patternmatching.html 作为 OCaml 正则表达式的示例,但我无法让它工作。

【问题讨论】:

  • 你能把 link 贴到你用来找出它支持的正则表达式语法的页面吗?谢谢!
  • 单词边界后面的斜线怎么办?
  • 不应该是"\\bfoo\\b"结尾不带斜线吗?
  • @bobblebubble 哎呀,当我对问题进行格式化时,最后的斜线是一个错字。不应该在那里。没有它仍然无法工作。

标签: regex ocaml reason


【解决方案1】:

在我看来,您好像误读了文档。 “Str.regexp_string s 返回与 s 完全匹配的正则表达式,仅此而已。”换句话说,使用你的reStr.string_match re "\\bfoo\\b" 0 将返回 true;并且您对re 的定义等同于Str.regexp "\\\\bfoo\\\\b"。你想要的是Str.regexp "\\bfoo\\b"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 2022-12-18
    • 2021-06-15
    • 2021-10-29
    相关资源
    最近更新 更多