(ReasonML) 正则表达式 \b 不匹配
(ReasonML) Regex \b not matching
我正在尝试 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. I was using http://pleac.sourceforge.net/pleac_ocaml/patternmatching.html 找到,作为 OCaml 正则表达式的示例我无法使用它。
我觉得你误读了文档。 “Str.regexp_string s
return 是一个完全匹配 s
的正则表达式,没有其他匹配。”换句话说,有了你的 re
,Str.string_match re "\bfoo\b" 0
就会 return 为真;并且您对 re
的定义等同于 Str.regexp "\\bfoo\\b"
。你想要的是 Str.regexp "\bfoo\b"
.
我正在尝试 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. I was using http://pleac.sourceforge.net/pleac_ocaml/patternmatching.html 找到,作为 OCaml 正则表达式的示例我无法使用它。
我觉得你误读了文档。 “Str.regexp_string s
return 是一个完全匹配 s
的正则表达式,没有其他匹配。”换句话说,有了你的 re
,Str.string_match re "\bfoo\b" 0
就会 return 为真;并且您对 re
的定义等同于 Str.regexp "\\bfoo\\b"
。你想要的是 Str.regexp "\bfoo\b"
.